Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kuphryn

Virtual Base Class & Constructor :: C++

This topic is 5852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
"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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!