multiple inheritence help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: multiple inheritence help

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Question multiple inheritence help

    #include <iostream>
    using namespace std;
    class A
    {
    public:
    int a;
    A() : a(1234) {};
    };


    class B : virtual public A
    {
    public:
    B() {a = 2345;};
    };

    class C : public A
    {
    public:
    C() {a=3456;};
    };

    class E: virtual public A
    {
    public:
    E() { a = 4567; };
    };

    class D: public B, public C
    {
    };

    class F: public D, public E
    {
    };
    void main()
    {
    F f;
    //HERE cout << f.a;
    cout << f.E::a << "\n";
    cout << f.D::B::a << "\n";
    cout << f.D::C::a << "\n";

    }


    1. When I comple the above code using cl.exe from microsoft command line compiler, it compiles find and the output is the following:
    4567
    4567
    3456

    When I compiler the above code using bcc32 (borland c++ command line compiler), I see the following
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    testmi.cpp:
    Error E2090 testmi.cpp 41: Qualifier 'B' is not a class or namespace name in fun
    ction main()
    Error E2090 testmi.cpp 42: Qualifier 'C' is not a class or namespace name in fun
    ction main()
    *** 2 errors in Compile ***


    Who is correct???

    2. I should have one virtual instance F:::B::A::a and F::E::A::a and one non-virtual instance through F:::C::A::a
    4567 is what E initialized a with, why do we see this? why no see what B initialized? How to initialize a in F or in D in the constructor?


    3. How to make //HERE commented out code compile and work, and refer to the one-virtual A instance, with out doing any using?? (is that possible?)

  2. #2
    Join Date
    Feb 2009
    Posts
    326

    Re: multiple inheritence help

    I compiled your code as it is, and I didn't get errors.
    I used gcc to compile the code.

    Given below is a program that might help to understand how memory is allocated in a virtual base, see if this helps:

    It displays the memory address in each case, making to understand better.

    Code:
    #include <iostream>
    using std :: cout;
    using std :: endl;
    
    class ClassA
    {
        public:
            int data;
    };
    
    class ClassB : public ClassA
    {};
    
    class ClassC : virtual public ClassA
    {};
    
    class ClassD : virtual public ClassA
    {};
    
    class ClassE : public ClassB, public ClassC, public ClassD
    {};
    
    
    int main()
    {
    
        system("clear");
    
        ClassE objE;  
    
        cout << "&(objE.ClassB :: data) = " << &(objE.ClassB :: data) << endl
             << "&(objE.ClassC :: data) = " << &(objE.ClassC :: data) << endl
             << "&(objE.ClassD :: data) = " << &(objE.ClassD :: data) << endl;
    
    
        return(0);
    }
    small suggestion:
    --------------------
    you could use code tags [code_] [/code_] (without the underscore) to retain the formatting.

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: multiple inheritence help

    1. Borland is an old compiler.

    2. The "E" part of "F" is initialized after the "D" part, and overwrites the value.

    3. "F" contains two members with the name "a". If you want to refer to one of them without fully qualifying the name, you're going to have to tell the compiler which.

    Why would you ever want to use something like this?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center