Thread: Basic Doubt in Virtual Classes

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Basic Doubt in Virtual Classes

    Code:
    class Base
    {
    public :
    int a;
    };
    
    class D1 : virtual public Base
    {
    public:
    int b;
    };
    
    class D2 : virtual public Base
    {
    public:
    int c;
    void change_a()
    {
    a=5;
    }
    };

    D2 makes a=5.
    What effect does this have on the versions of "a" in Base and D1?
    How is memory allocated actually in this program...
    ?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In this setup D1 is unrelated to D2, so changing a in an instance of D2 will have no effect on anything that is a D1. Did you mean to add a third class that inherits from both D1 and D2?

  3. #3
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    Yes! How did you know? Is it a typical example? or is there some other reason?
    What will be the situation then?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> How did you know?
    Because a virtual base class doesn't make sense unless you are using multiple inheritance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  2. virtual classes and inheritance
    By skora in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 05:28 PM
  3. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  4. virtual or pure virtual
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2001, 07:19 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM