Polymoprhism - subclasses sharing of baseclass private members

79 Views Asked by At

Why doesnt the subclasses share the same private membervariable in the superclass using polymoprhism? There are only one instance of the baseclass and if SubA is setting the private member through a mutator - why then cannot SubB access this value. How would it look like if I want the subclasses to share the same private member?

 #include <iostream>
class Super {

   private:
     int cnt;
   public:
     int getCnt() {
        return cnt;
     }
     void setCnt(int cnt) {
        this->cnt = cnt;
     }
};

class SubA: public Super {

};

class SubB: public Super {

};

int main() {

  Super *super;
  SubA a;
  SubB b;

  super = &a;
  super->setCnt(10);
  super = &b;
  std::cout << super->getCnt() << std::endl;
  super = &a;
  std::cout << super->getCnt() << std::endl;

 return 0;
}

produces:

-8589546555 (garbage)
10
2

There are 2 best solutions below

0
On

Why doesnt the subclasses share the same private membervariable in the superclass using polymoprhism?

Polymorphism has nothing to do with how data is embedded in classes; (run-time) polymorphism is only relevant to virtual dispatch and Run-Time Type Information (RTTI) for dynamic_cast and type_info.

If you imagine your a and b objects on the stack, their memory layout can be illustrated like this:

A:[[Super: int cnt;]A-specific fields (if there were any)]
B:[[Super: int cnt;]B-specific fields (if there were any)]

In effect, class A : public Super is saying "I may want to extend 'Super', optionally appending my own data members to those in Super, possibly adding further functions / overriding virtual ones".

There are only one instance of the baseclass and if SubA is setting the private member through a mutator - why then cannot SubB access this value.

This is wrong... each sub class embeds its own instance of the super class.

How would it look like if I want the subclasses to share the same private member?

Well, there are lots of ways in which you could orchestrate that:

  • You could have the subclasses hold pointers to a Data object into which you move the superclass's data you want shared, then you'd need a way to initialise the Data object and make it known to all the subclass instances you want to share it.

  • You could make the superclass data static, which means a single copy of each static variable will be shared by all instances of the superclass, whether embedded in subclasses or not.

4
On

There are only one instance of the baseclass and if SubA

That is wrong. a and b are different objects. They each have an instance of an A sub object. You have not set cnt in b, so it is no surprise that looking at it gives you a garbage value, because reading from an uninitialized object is undefined behaviour.

How would it look like if I want the subclasses to share the same private member?

You could give the base class a static data member. That means all instances of A would share the same member.