Thread: Virtual Base Class & Constructor :: C++

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Virtual Base Class & Constructor :: C++

    Hi.

    I would like to know the best way to initialize a constructor in a virtual base base. For example,

    Code:
    // 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.

    Thanks,
    Kuphryn
    Last edited by kuphryn; 09-13-2002 at 01:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Not 100% sure when to use virtual methods in base class
    By Silvercord in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 03:19 PM
  5. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM