CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2001
    Location
    Turkey
    Posts
    173

    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{;};
    };

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902
    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
  •  





Click Here to Expand Forum to Full Width

Featured