A question regarding inheritance implementation. Let's say we have 2 classes:

class B
{
public:
     int x;   // for the sake of argument it's public, don't stone me 
};

class D: public B
{
public:
     int x;   // again x, i know ... stupid dilemma
};

Now, if you will use a sizeof on the newly created type D you'll see it has a size of 8 bytes that means that the created D class has 2 x members which means in my opinion that the class looks something like that:

class D
{
public:
    int x;
    int x;   //  problem !!!
}

Now for member functions this ain't a problem, i know that they are not part of the object and the this pointer solves the problem regarding who to call(taking in account the vtable), but what about the member variables? How does the compiler implement behind the curtain the derived D class in such a way that there's no conflict of names.
Is the member variable from the base class renamed? I know that in C++ you can simply call B::x and the compliler knows what you mean but how would this be implemented in C let's say ...? How would the class D as struct look ?
To make it short , how is actually INHERITANCE IMPLEMENTED?

P.S. I know it's compiler stuff and compiler dependent but my guess is that all compilers somehow go the same way so it's a pretty general question and ... it simply interests me.

Thx for all reply's

Without actually knowing how it is implemented and by only playing around with these two classes it seems when there is a name conflict (in this case x) the default scope to be used is that of the class that is calling it. When inheriting another class the class that is being inherited is added to the scope of the "inheriter" if there is no conflict.

For example

class B
{
	public:
	int x;
};

class D: public B
{
	public:
	int x;
};

would result in 'D' looking like

class D: public B
{
	public:
	int D::x; //in the default scope so 'D d.x' would look at this
	int B::x; //name conflict so would need to use 'D d.B::x' to access 
};

As for implementing a structure like this in C I would say it is impossible to put two variables with the same name into the same structure since there are no namespaces or 'built-in' inheritance in C. I came across this page and it goes over how to make classes with functions and inheritance in C (I didn't read it that much just scrolled through it).

> Is the member variable from the base class renamed?

No. There is no need to rename it; the fully qualified name of x in A is A::x and the fully qualified name of x in B is B::x.

Normal scoping rules apply for name look up; if a name is not found in an inner scope, the outer scope is looked up. The name of the base class behaves in a way similar to an outer scope.

#include <iostream>

struct A
{
    int x ;
    int y ;
};

struct B : A
{
    int x ;
    void foo( int x ) ;
};

void B::foo( int x )
{
    std::cout << x << ' ' // local variable 'x'
              << B::x << ' ' // member variable 'x' in B
              << A::x << '\n' ; // member variable 'x' inherited from A

    std::cout << y << ' ' // no local variable 'y', this->y
              << B::y << ' ' // no member variable 'y' in B, so A::y
              << A::y << '\n' ; // member variable 'y' in A (inherited by B)
}

> but how would this be implemented in C let's say ...? How would the class D as struct look ?
> To make it short , how is actually INHERITANCE IMPLEMENTED?
> ..my guess is that all compilers somehow go the same way ...

Compilers implement it by having an anonymous base class sub-object in the derived class object. (A derived class constructors first invokes the base class constructor in order to initialize this object.)


> how would this be implemented in C let's say ...?

In C, this can be simulated (except for the automatic base class member lookup) by

typedef struct AA AA ;
struct AA
{
    int x ;
};

typedef struct BB BB ;
struct BB // : AA
{
    AA anonymous_base_class_subobject ;
    int x ;
};

Thx for the reply, the link was useful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.