-
February 16th, 2004, 10:13 AM
#1
base class initialization
The last class doesn't compile.
Any suggestion ?
I've tried both
CDerivedEx( int x, CString s, int a ) : CDerived( s, a ) : CBase( a )
and
CDerivedEx( int x, CString s, int a ) : CDerived( s, a ) , CBase( a )
Both gets error! ??
class CBase
{
CBase( int a ){;};
CBase( CString s ){;};
~CBase(){;};
};
class CDerived : public CBase
{
CDerived( CString s, int a ) : CBase( s ) {;}; //with string
~CDerived( ){;};
};
class CDerivedEx : public CDerived
{
CDerivedEx( int x, CString s, int a ) : CDerived( s, a ) : CBase( a ){;}; // with int
~CDerivedEx{;};
};
-
February 16th, 2004, 10:33 AM
#2
You can only initialize your immediate base class.
You have to provide multiple constructors to give "access" to your base class's constructors.
For example:
Code:
class CBase
{
public:
CBase(int a) {}
CBase(CString s) {}
~CBase() {}
};//CBase
class CDerived : public CBase
{
public:
CDerived(CString s) : CBase(s) {} //with CString
CDerived(int a) : CBase(a) {} //with int
~CDerived() {}
};//CDerived
class CDerivedEx : public CDerived
{
public:
CDerivedEx(int x, CString s) : CDerived(s) {} // with CString
CDerivedEx(int x, int a) : CDerived(a) {} // with int
~CDerivedEx() {}
};//CDerivedEx
If this doesn't meat your needs, then don't use constructors to construct your object. For example, alot of MFC objects use Create() or some other method to officially "construct" the object into something usable.
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|