tags:

views:

107

answers:

4

Hi,

I would like to know how c++ ensures the concept layout in memory of these classes to support inheritance.

for example:

class Base1
{

    public:
        void function1(){cout<<"Base1"};

};

class Base2
{

    public:
        void function2(){cout<<"Base2"};

};

class MDerived: Base1,Base2
{

    public:
        void function1(){cout<<"MDerived"};

};

void function(Base1 *b1)
{

    b1->function1();
}

So when I pass function an object of derived type the function should offset into the base1 class function and call it. How does C++ ensure such a layout.

A: 

The layout of a class in memory includes its members and base class subobjects (§10/2). Members are also subobjects. A pointer to a base subobject is a pointer to an object (but not a most-derived object).

When you convert an MDerived * to a Base2 *, the compiler looks up the offset of the Base2 object inside the MDerived object and uses it to generate this for the inherited method.

Potatoswatter
how does the compiler know about the offset. Does it store somewhere ?
mousey
It's part of the definition of the structure. It's remembered by the compiler, but not directly stored in the compiled program. You could say it's "stored" in the ADD instruction that generates the base pointer from the derived pointer.
Potatoswatter
A: 

I think you're asking why, when you call b1->function(), does Base1::function1() fire?

If so, then the reason is because b1 is a Base1 pointer, not a MDerived pointer. The object it points to may in fact "be" an MDerived object, but function(Base1*) has no way of knowing this, so it calls the only thing it does know -- Base1::function1().

Now, if you had marked the base class function as virtual, things change:

#include <iostream>
#include <string>
using namespace std;

class Base1 
{
public: virtual void function1() { cout<<"Base1"; }
};

class Base2 
{
public: void function2(){cout<<"Base2";}
};

class MDerived: public Base1, public Base2 
{
public: void function1(){cout<<"MDerived";}
};

void function(Base1 *b1) 
{
    b1->function1();
}

int main()
{
    MDerived d;
    function(&d);
}

The output of the program is:

"MDerived"

void function(Base1 *b1) still doesn't know that the object being pointed to is actually an MDerived, but now it doesn't have to. WHen you call virtual functions through a base class pointer, you get polymorphic behavior. Which in this case means MDerived::function1() is called because that is the most-derived type available.

John Dibling
A: 

Few things in your code

  1. The multiple inheritance should be public, else the compiler will complain that there is no access to the base classes
  2. No ; at the end of the cout statements
  3. Did not include the <iostream> (ok may be I am being too pedantic)

To answer your question - the compiler knows that it is the base class because the type of the argument that the function is taking is Base1. The compiler converts the type of the passed data (assuming you passed a derived object, does object slicing) and then calls the function1() on it (which is a simple offset from the base pointer calculation).

Gangadhar
A: 

When a MDerived* needs to be converted to a Base1*, the compiler adjusts the pointer to point to the correct memory address, where the members of this base class are located. This means that a MDerived* that is cast to a Base1* might point to a different memory address than the original MDerived* (depending on the memory layout of the derived class).

The compiler can do this because it knows the memory layout of all the classes, and when a cast occurs it can add code that adjusts the address of the pointer.

For example this might print different addresses:

int main() {
   MDerived *d = new MDerived;
   std::cout << "derived: " << d << std::endl;
   std::cout << "base1: " << (base1*)d << std::endl;
   std::cout << "base2: " << (base2*)d << std::endl;
}

In your example such adjustments might not be necessary since the classes don't contain any member variables that would use any memory in the sub-objects representing the base classes. If you have a pointer pointing to "nothing" (no member variables), it doesn't really matter if that nothing is called Base1 or Base2 or MDerived.

The non-virtual methods of the classes are not stored with each object, they are stored only once. The compiler then statically, at compile time, uses those global addresses when a member function is called, according to the type of the variable used.

sth