P
Paul Richards
Hi,
I remember reading something regarding this in one of my C++
textbooks, but unfortunately I do not have those available to me right
now..
Say I have some classes which do a little bit of funky MI...
class BaseA {
.....
};
class BaseB {
.....
};
class Derrived : public BaseA, public BaseB {
.....
};
Say I have a pointer to Derrived as such:
Derrived *d = new Derrived();
I can cast it to either BaseA* or BaseB* of course very easily:
BaseA *a = d;
BaseB *b = d;
What I can't remember is whether it'll work to cast BaseA* to/from
BaseB* assuming I know that I really do have an instance of Derrived..
[style A]: a = (BaseA*)b; // is this ok?
I have a feeling that this isn't safe. Do I need to use dynamic_cast
down to Derrived* then back up?
[style B]: a = dynamic_cast<Derived*>( b );
Or am I getting mixed up, is style A safe so long as my inheritances
aren't virtual?
I remember reading something regarding this in one of my C++
textbooks, but unfortunately I do not have those available to me right
now..
Say I have some classes which do a little bit of funky MI...
class BaseA {
.....
};
class BaseB {
.....
};
class Derrived : public BaseA, public BaseB {
.....
};
Say I have a pointer to Derrived as such:
Derrived *d = new Derrived();
I can cast it to either BaseA* or BaseB* of course very easily:
BaseA *a = d;
BaseB *b = d;
What I can't remember is whether it'll work to cast BaseA* to/from
BaseB* assuming I know that I really do have an instance of Derrived..
[style A]: a = (BaseA*)b; // is this ok?
I have a feeling that this isn't safe. Do I need to use dynamic_cast
down to Derrived* then back up?
[style B]: a = dynamic_cast<Derived*>( b );
Or am I getting mixed up, is style A safe so long as my inheritances
aren't virtual?