From: Brad Gillespie Brad on
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!
From: David Scambler on
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
Gillespie(a)discussions.microsoft.com> wrote in
news:7BD4C947-8906-4720-BB74-85EF784318A3(a)microsoft.com:

> 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.
>
>

From: Brad Gillespie on
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.


"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.
>
>
>
> 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!
From: David Scambler on
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;
>
> 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.
>
>
>

From: David Scambler on
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
};