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!
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:
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.
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;
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
};
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.
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