Virtual Base Class & Constructor :: C++

Started by
3 comments, last by kuphryn 21 years, 8 months ago
Hi. I would like to know the best way to initialize a constructor in a virtual base base. For example, ------------------------------------------------------------ // Base class class Base() { public: A(int i = 0); virtual void printA() const = 0; }; // Derived classes class X() : public virtual Base { public: X(char a, int j); virtual void printA(); }; X::X(char a, int j) { char temp = a; A::A(j); } class Y() : public virtual Base { public: Y(long x, int k); virtual void printA(); }; Y::Y(long x, int k) { long temp = x; A::A(k); } // Next level down in hierarchy class Z(); public X, public Y { public: Z(...?...); // What data do I need here to construct X, Y, and Base? void printA() const; ... }; Z::Z(...?...) { // Do you contruct class X, Y, or Base first? } // Implementation Z example(...?...); // You need to construct Base first. ---------------------------------------------------------- I know the question is very basic to polymorphic C++ programming. Nonetheless, it is something that want really want to understand.
Advertisement
a) Virtual bases are always initialised before non-virtual bases, depth-first, left to right
b) Base classes are always initialised before member variables, in the order in which they appear in the inheritance specification
c) Member variables are always initialised in the order of declaration in the class body
d) You should use an initialiser list, not initialise your parameters within the constructor body.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Okay. Thanks.

Can you elaborate on as to what you mean about this statement?

d) You should use an initialiser list, not initialise your parameters within the constructor body.

I still would like to know how to initialize a virtual base class.

Thanks,
Kuphryn
"Can you elaborate on as to what you mean about this statement?

d) You should use an initialiser list, not initialise your parameters within the constructor body."

The initializer list is inbetween the closing parentheses of your constructor and the opening bracket of your constructor. Like so:

class CPants
{
int m_iSize;
CBody *m_pParent;
CPants( int size, CBody *pParent);
}
CPants::CPants( int size, CBody *pParent ) : m_pParent(pParent)
{
m_iSize = size;
//rest of constructor
}

/*-=-=-=-=-=-=-=-=-=-=-=-=
trae@illicitstudios.com
=-=-=-=-=-=-=-=-=-=-=-=-*/

[edited by - taciturn on September 13, 2002 5:16:52 PM]

[edited by - taciturn on September 13, 2002 5:17:23 PM]
/*-=-=-=-=-=-=-=-=-=-=-=-= trae@illicitstudios.com=-=-=-=-=-=-=-=-=-=-=-=-*/
Ohhhhhh. Yeah. The sample I type had some errors.

Thanks,
Kuphryn

This topic is closed to new replies.

Advertisement