Question

When I looked at the following code sample, I thought d.B::num was the same as b.num but it isn't. It appears to be a different variable that has its own address. When I click on the run button, I see each of the three variables b.num, d.num and d.B::num has its associated value (3, 4 and 5 respectively).

Why is that so? And what exactly is d.B::num here, if it's not the same as b.num?

struct B {int num;};

struct D : public B {int num;};

int main() {

  B b;
  D d;

  b.num = 3;
  d.num = 4;
  d.B::num = 5;

  cout << b.num << endl;
  cout << d.num << endl;
  cout << d.B::num << endl;

  return 0;
}
Was it helpful?

Solution 2

There are instances of classes, and there are classes. They are different things. Much like how all integers are not the same value, different instances of a class are not the same value.

When a derived class inherits from a base class, the instances of the derived class have an instance (a subinstance really) of the base class within them.

If there is no use of the virtual key word, those sub instances of the derived class instance are basically perfectly normal instances of a base class. When you create a member variable of the same name in derived as you have in base, all you do is hide the variable in the subinstance of base from casual use. You can still get at the subinstances hidden variable by either accessing it through a pointer or reference to base, or by fully qualifying it with the Base::x syntax.

Despite looking like accessing a static variable, the Base::x syntax is also used to refer to names of things in base that may be hidden in derived, even if they are not static.

I mentioned this was only true for non-virtual cases. Now, virtual methods in base can be overriden in derived. You can think of virtual methods as being pointers to the actual methid, stored in base: wben you construct the derived instance, it goes and changes what the derived instance's base subobject's virtual method pointers point to to the methods of derived. After that happens, even when you have a pointer to base, calling a virtual method can call a derived method.

The ofther use of virtual is when you inherit. If you inherit without virtual, it is like the base class instances are concatenated in order as described by the order of inheritance, at the 'start' of the instance. If you inherit with virtual, instead there is a table of offsets that say where the base instance is relative to the derived object. This is mainly important in that you can have multiple subinstances of base in a given derived without virtual, but only one wirh virtual inheritance.

Some of the above is not exactly as dictated by the standard, but rather is how a compiler might implement the standards for illustrative purposes.

OTHER TIPS

It seems you have:

B[int B::num]

and

D[int B::num, int D::num]

if you call d.num it defaults to D::num if you call d.B::num it defaults to B::num

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top