Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloaded constructor issues in MFC Dialogs

78 views
Skip to first unread message

Gillespie@discussions.microsoft.com Brad Gillespie

unread,
Feb 15, 2010, 4:05:01 PM2/15/10
to
Thus far, I have been creating classes that have objects in the header file.
E.g., I might create a dialog box class, and in the *.h file for the dialog
box class, I have instantiated various objects, such as tab controls or list
controls, etc. So the dialog class “has-a” list control, for instance.

However, I have run into a severe limitation. I can’t use alternative
constructors (Overloaded Constructors) when doing this, because I can’t
include any pass parameters when instantiating an object in the *.h file of
some other class.

Let me clarify. If I create a dialog class called DialogA, then in the *.h
file for that class, I cannot instantiate a member object by passing
variables to the constructor of that object’s class. That is, in the
DialogA.h file,

CMyListCtrl ListCtrlObj; // I can say this

CMyListCtrl ListCtrlObj(123,’M’); // I can’t say this

However, in the DialogA.cpp file, it is entirely possible to instantiate an
object using whatever pass parameters you want to. I.e., you can say things
like

CMyListCtrl ListCtrlObj(123,’M’);

all day long.

But if you do that, obviously the object will not be a member variable of
the dialog class, it will simply be a local variable in one of the functions.

Whatam I missing here? I must not realize some fairly major philosophical
point? How do I instantiate various version of the same object (using pass
parameters) but still have those objects as member variables of some other
object (like a dialog class)?

Help!

David Scambler

unread,
Feb 15, 2010, 5:05:59 PM2/15/10
to
Method 1:
Make the required parameters of the member class constructor also
parameters of the containing class constructor so that the containing
class constructor can initialize the member class in its initialization
list.
This may just have the effect of shifting the problem to the containing
class, but the very nature of the problem you pose entails knowing all
parameters of all member instances in advance. So at some point you must
instantiate an uber class and provide it with all the parameters for all
member classes.

Method 2:
Construct the member with no parameters but give it a method you can
call to initialize it. i.e. two-phase construction.


=?Utf-8?B?QnJhZCBHaWxsZXNwaWU=?= <Brad
Gill...@discussions.microsoft.com> wrote in
news:7BD4C947-8906-4720...@microsoft.com:

Brad Gillespie

unread,
Feb 15, 2010, 5:28:04 PM2/15/10
to
More detail: same question:

Simpler version of the same question:

Suppose you have a class MyClass. MyClass has two alternative constructors,
like this:

MyClass(int k);
MyClass(CString m);

Now you have another class, called MyProject.

You can't put a MyClass object in MyProject.h. That is, you can't say

MyClass MyObject(47);

in MyProject.h. It won't let you. It seems completely crazy that you can't
use
alternative constructors when putting an object into another class. You
can't put
anything in the pass parameter field when declaring an object in the *.h
file of
another class. This seems insane, so I must be missing something.

David Scambler

unread,
Feb 15, 2010, 5:58:23 PM2/15/10
to
Correct. You cannot do that.

If the parameter is constant as in your example you can do this:

#include "MyClass.h"
class MyProject
{
public:
MyProject() : MyObject(47)
{
}
...
MyClass MyObject;
...
};


But normally the MyProject constructor code including its initialization
list would be in the cpp, not the header. Instantiation is a run time
process, not compile time.

If you want a header with a constant parameter (e.g. 47) in it then you
are specifying that all users of that header get a MyObject(47). In
which case in C++ you could do:

class MyClass47 : public MyClass
{
MyClass47() : MyClass(47) {}
};

#include "MyClass47.h"
class MyProject
{
public:
MyClass47 MyObject;
...
};

> Suppose you have a class MyClass. MyClass has two alternative
> constructors, like this:
>
> MyClass(int k);
> MyClass(CString m);
>
> Now you have another class, called MyProject.
>
> You can't put a MyClass object in MyProject.h. That is, you can't say
>

> MyClass MyObject;

David Scambler

unread,
Feb 15, 2010, 6:50:31 PM2/15/10
to
For constant integer parameters you could use a template.

class MyClass
{
public:
MyClass(); // default init
MyClass(unsigned int n); // init using n
MyClass(LPCTSTR p); // init using p
};


template <unsigned int Param>
class MyClassT : public MyClass
{
public:
MyClassT() : MyClass(Param) {}
};

class MyProject
{
public:
...
protected:
MyClass MyObject;
MyClassT<123> MyObject1; // can do this
MyClassT<42> MyObject2; // or this

MyClassT<_T("Hello")> MyObject3; // syntax - cannot do this
MyClass MyObject4(_T("Hello")); // syntax - cannot do this
};

Goran

unread,
Feb 16, 2010, 2:26:48 AM2/16/10
to
On Feb 15, 10:05 pm, Brad Gillespie <Brad

Gilles...@discussions.microsoft.com> wrote:
> Thus far, I have been creating classes that have objects in the header file.  
> E.g., I might create a dialog box class, and in the *.h file for the dialog
> box class, I have instantiated various objects, such as tab controls or list
> controls, etc.  So the dialog class “has-a” list control, for instance.
>
> However, I have run into a severe limitation.  I can’t use alternative
> constructors (Overloaded Constructors) when doing this, because I can’t
> include any pass parameters when instantiating an object in the *.h file of
> some other class.
>
> Let me clarify.  If I create a dialog class called DialogA, then in the *.h
> file for that class, I cannot instantiate a member object by passing
> variables to the constructor of that object’s class.  That is, in the
> DialogA.h file,
>
> CMyListCtrl  ListCtrlObj;                      // I can say this
>
> CMyListCtrl  ListCtrlObj(123,’M’);       // I can’t say this

It looks like you are looking for this:

header:
class CMyDialog : public CDialog
{
public:
CMyDialog(CWnd* pParent, int listParam1, char ListParam2);
}

implementation (or inline):
CMyDialog::CMyDialog(CWnd* pParent, int listParam1, char listParam2) :
CDialog(IDD, pParent), ListCtrlObj(listParam1, listParam2)
{}

Please don't take offense, but it also looks like you should not be
using MFC yet, you should instead learn more about the language
itself.

Goran.

David Wilkinson

unread,
Feb 16, 2010, 6:41:18 AM2/16/10
to
Brad Gillespie wrote:
> Thus far, I have been creating classes that have objects in the header file.
> E.g., I might create a dialog box class, and in the *.h file for the dialog
> box class, I have instantiated various objects, such as tab controls or list
> controls, etc. So the dialog class “has-a†list control, for instance.
>
> However, I have run into a severe limitation. I can’t use alternative
> constructors (Overloaded Constructors) when doing this, because I can’t
> include any pass parameters when instantiating an object in the *.h file of
> some other class.

Brad:

The concept others are telling you about is called the "initializer list".

You are probably familiar with providing parameters for the base class in the
initializer list, but what you are missing is that you can also provide
parameters for members. The different items (base and members) are separated by
commas.

There is also something wrong with your terminology:

First of all, when you say "in the .h file", you really mean "in the class
definition". Class definitions do not have to be in a .h file, though this is a
common way of organizing code.

Second, you do not "create" member objects in the class definition, you
"declare" them. They are not created until the object itself is created, and
that is why the constructor parameters are supplied in the initializer list, not
in the class definition.

--
David Wilkinson
Visual C++ MVP

0 new messages