How to assign a base object to a derived object

I have a question about C ++, how to assign a base object to a derived object? or how to assign a pointer to a base object to a pointer to a derived object?

In the code below, two lines are erroneous. How to fix this?

#include <iostream> using namespace std; class A{ public: int a; }; class B:public A{ public: int b; }; int main(){ A a; B b; b = a; //what happend? cout << bb << endl; B* b2; b2 = &a; // what happened? cout << b->b << endl; } 
+4
source share
5 answers

When an object is on the stack, you can really assign objects of the same type to each other. They can be converted via overloaded casting operators or overloaded assignment operators, but you specify the conversion at this point. The compiler cannot independently perform such conversions.

 A a; B b; b = a; 

In this case, you are trying to assign A to B, but A is not B, so it does not work.

 A a; B b; a = b; 

It works in fashion, but probably it will not be what you expect. You just chopped your B. B is A, so the assignment may take place, but because it is on the stack, it just assigns parts b that are part A. So what you get is A. not B, even though you assigned from B.

If you really want to assign objects of one type to another, they must be pointers.

 A* pa = NULL; B* pb = new B; pa = pb; 

It works. pa now points to pb, so it is still B. If you have virtual functions on A and B, they overlap them, then when you call them on pa, they will call version B (non-virtual will still call version A).

 A* pa = new A; B* pb = pa; 

This does not work. pa does not indicate B, so you cannot assign it to pb, which should point to B. Just because B is A, this does not mean that A is B.

 A a; B* pb = &a; 

This does not work for the same reason as the previous one. It so happened that A on the stack this time instead of a heap.

 A* pa; B b; pa = &b; 

It works. b is B, which is A, so A can point to it. Virtual functions will call version B, not virtual functions will call version A.

So basically, A * can point to B because B is A. B * cannot point to A because it is not B.

+2
source

It makes no sense to assign a base object to a derived one (or a base pointer to a derived pointer), so C ++ will do everything possible to prevent you from doing this. The exception is that the base pointer really points to the derived one, in which case you can use dynamic casting:

 base * p = new derived; derived * d = dynamic_cast <derived *>( p ); 

In this case, if p really points to the base, the pointer d will contain NULL.

+4
source

The compiler will not allow this kind of thing. And even if you manage to do it through some kind of casting, doing it does not make sense. Assigning a derived object to the base pointer makes sense, because whatever the base can do can do it. However, if the opposite was allowed, what should you do if you are trying to access a member defined in a derivative on a base object? You are trying to access a memory area filled with garbage or irrelevant data.

+2
source
 b = a; //what happend? 

It is illegal - A is not B, so you cannot do this.

 b2 = &a; // what happened? 

The same thing here.

In any case, the compiler does not know what to assign int b , so it prevents you from doing this. Another way (assigning Derived to Base) works because Base is a subset of Derived.

Now, if you tell us exactly what you want to achieve, we can help you.

If this is a case of assigning A, which is known to be derived, you can cast:

 A* a = new B(); B* b = dynamic_cast<B>(a); 

Just remember that if a not B , then dynamic_cast will return NULL. Please note that this method only works on pointers for some reason.

+1
source

A derived object is a kind of basic object, and not vice versa.

0
source

Source: https://habr.com/ru/post/1299795/


All Articles