Reputation: 355
Let's say I have a class A that inherits from its parent class B, and there is another class C that also inherits from class B. Is there a way to change this pointer of class A to class C at run time?
class A : public B {
A::someFunction() {
//can I change this pointer to class C here?
}
}
class C : public B {
...
}
Upvotes: 0
Views: 143
Reputation: 238341
Is there a way to dynamically change an object to another type?
No. The type of an object cannot change through its lifetime.
Let's say I have a class A that inherits from its parent class B, and there is another class C that also inherits from class B. Is there a way to change this pointer of class A to class C at run time?
No.
At best, you could destroy the original object, and reuse its memory to create another object. Obviously the size and alignment of the memory must be sufficient for the new type. Any reference (which includes pointers such as this
in a member function) to the old object will have been invalidated by the destruction of the original object. Reuse of storage is an advanced topic which I don't recommend to beginners.
Upvotes: 1
Reputation: 10606
There is no valid way to do this for one simple reason - in your class inheritance structure, an object of class A
cannot be also an object of class C
. In C++, two objects of different types can have the same address if and only if one of the objects is a subobject of the other, which is not your case. If you cast a pointer to A
to a pointer to C
, the pointer will still refer to an object of type A
, and dereferencing the casted pointer would result in undefined behavior.
Most probably, what you want to do is to create an object of class C
from an object of class A
. You can use a converting constructor or a conversion operator to implement this.
Note that if what you really want is to reuse the storage allocated for the object of type A
, you will still need to destroy the object A
first and then construct the object C
. You will have to save any relevant data from A
before destroying it to be able to construct C
with the data.
Upvotes: 0
Reputation: 3372
You cant and you shouldn't. The reason is pretty simple. Take a look at this code,
class Base {
public:
Base() {}
virtual void SayHello() {}
};
class A_Derived : public Base {
public:
A_Derived() {}
virtual void SayHello() override { ... }
void SayAllo() { ... }
};
class B_Derived : public Base {
public:
B_Derived() {}
virtual void SayHello() override { ... }
void SayBello() { ... }
};
Now when is we assign the A_Derived
class pointer to B_Derived
, the compiler will allow to call the SayBello
method. This is because for the compiler, its a B_Derived
class pointer, it doesn't know about the actual pointer data is pointing at a data block of A_Derived
(because inheritance is not compile time, its runtime). So what happens when you call SayBello
using that pointer? Its gonna be undefined behavior. You see the issue?
This is why you cant do it (logically and also using C++ style casting).
Upvotes: 1