Virtual Base Class & Constructor :: C++

This is a discussion on Virtual Base Class & Constructor :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. I would like to know the best way to initialize a constructor in a virtual base base. For example, ...

  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.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Of course it depends on the actual classes you would use, but basically, each class should pass the required parameters to its parent constructor. If you do that, you don't have to worry about your child class's parent's parents.
    And when you call it, it should be in your initializer list and not inside the body of the constructor.

    Code:
    ///////////////////////
    class Base {
      int _base;
    public:
      Base(int base);
      virtual Base() = 0 { }
    };
    
    Base::Base(int base) 
    : _base(base) {
    
    }
    ///////////////////////
    class X : public Base {
      int _x;
    public:
      X(int x);
    };
    
    X::X(int x)
    : _x(x), Base(x)//initialize here...
    {
      //...not in here
    }
    ///////////////////////
    class Y : public Base {
      int _y;
    public:
      Y(int y);
    };
    
    Y::Y(int y)
    : _y(y), Base(y) {
    
    }
    ///////////////////////
    class Z : public X, public Y {
      int _z;
    public:
      Z(int z);
    };
    
    Z::Z(int z)
    : _z(z), X(z), Y(z) {//doesn't matter which you call first
    
    }
    Hope it helps

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Nice! Thanks.

    Kuphryn

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, 04: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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21