2

Can any explain why baseclass pointer cannot be assigned to derived class pointer? I know it is not possible ,but i like to know the reason .logically derived class contain base class.Let me know the reason Thanks in advance

3
  • 1
    All cats are animals, but not all animals are cats. Assigning an animal to a pointer to a cat doesn't necessarily make sense. Commented Dec 9, 2014 at 5:16
  • Logically derived class instance is also base class instance, but not every base class instance is a derived class instance. Pointer to base cannot be assigned to pointer to derived because pointer to base class doesn't have to point to a derived class instance (it might just be a base class, or another derivative of base class instance); assigning such base pointer to derived pointer could allow pointer to derived to point to something that is base but not derived (as original base pointer was). Commented Dec 9, 2014 at 5:21
  • and if you want to see if that animal is a cat, use a dynamic_cast<> Commented Dec 9, 2014 at 9:16

3 Answers 3

1

Consider:

struct A
{
};

struct B : A
{
   int b;
};

struct c : A
{
   char c;
};

C cobj;
A* aptr = &cobj;
B* bptr = aptr; // Assuming this were allowed...
bptr->b = 10;

By doing that you'd have used memory beyond what is valid. You have created an object of size sizeof(C) but not you are treating it like it is of size sizeof(B).

Sign up to request clarification or add additional context in comments.

Comments

0

A derived class can have its own data members and member functions in addition to all of those within the base class.

If you point the derived class pointer to the same thing as a base class pointer, the derived class pointer will think it's pointing to a derived class object, even though it's pointing to a base class object.

If you try to access derived class data members or member functions using this derived class pointer, it won't work because it's pointing to a base class object that doesn't have these capabilities.

For instance, let's assume I have a base class called Person and a derived class called Programmer. I can make a Person object and have a pointer point to that object. I can then try to make a Programmer pointer point to that Person object as well (by setting it equal to the Person pointer). If I used my Programmer pointer to try and make the Person object code (equivalent to calling a Programmer member function) or do something only a Programmer does, it wouldn't work because the Person hasn't been specialized.

Comments

0

I assume your question is why following should not be done:-

Derived *ptr = new Base;   //assume for now public inheritance.

Derived is specialization of Base i.e Derived would inherit all features of base plus it could also add some of its own features. That means assigning Base to Derived ptr leads to loss of those features which could result into undefined behavior if call is made to that variable/method.

2 Comments

Not just loss of the features, but undefined behavior if the code were to try to use e.g. a member variable that doesn't exist.
@MarkRansom yeah right, I omitted it as it was just a consequence of former...updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.