Jump to content
  • Advertisement
Sign in to follow this  

C++ How derived to base

This topic is 3690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well My question is about compiler internals. suppose My base class have an int a; so size of base class is 4 bytes and my derived class has another int b; So I think Size of b is 8 (However I dont think compiler can be designed such that real size of the derived is 4 bytes). Now when a Derived Object is converted to Base the b is not lost cause we can still access b after we do dynamic_cast so what happens with that b when a derived class becomes Base Class.

Share this post

Link to post
Share on other sites
No, when the derived object is converted to a base object, "b" is discarded.
struct Base
int a;
struct Derived : public Base
int b;

Base objBase;
Derived objDerived;

objBase = objDerived;// 'a' is copied, 'b' is ignored
//the dynamic cast will fail! objBase is not a 'Derived'
Derived * ptr = dynamic_cast<Derived *>( &objBase );
assert( ptr == NULL );

I think you are being confused by pointers - in the following case, 'b' is preserved. It is not copied or discarded, it stays in the same memory location.
Derived  objDerived;
Base* pObjBase;

pObjBase = &objDerived;// the address is copied (not the a/b data)!
//the dynamic cast will succeed! pObjBase *is* a 'Derived'
Derived * ptr = dynamic_cast<Derived *>( pObjBase );
assert( ptr != NULL );

//N.B. ptr, &objDerived and pObjBase all point to the same memory location.
ptr->b = 42;
assert( objDerived.b == 42 )

Share this post

Link to post
Share on other sites
pObjBase = &objDerived;// the address is copied (not the a/b data)!

only the address is copied.
so there must be some way to get pObjBase.b Indirectly??
as pObjBase is pointing to such an address that really holds a b
However we are restricted to access it directly by doing pObjBase.b

But can we do any kind of pointer arithmatic to get b from pObjBase.
I belief its possible cause Its not absurd.

Its logical to restrict us from using pObjBase.b to get b
but my question is how it restricts us from using pObjBase.b

Share this post

Link to post
Share on other sites
The compiler doesn't "restrict" access to anything, ever. It just only compiles code that either: A) it knows how to generate output for that is 100% guaranteed correct (the type information of an object), or B) it knows how to generate code, assuming what you've told it is correct (casts, etc).

First, to be closer to the normal reality, assume all your objects are 4 bytes bigger than their data adds up to ... because if you use any virtual functions they are (usually the first 4 bytes are a vtable pointer). So the memory of an object base looks like:

0-3 -> pointer to "base" vtable
4-7 -> int a

and a derived object looks like

0-3 -> pointer to "derived" vtable
4-7 -> int a
8-13 -> int b

all that I'm about to say only applies IF YOU AREN'T USING MULTIPLE INHERITANCE (which is really a bit tricky and not worth my time right now).

when you cast a pointer from derived to base, it doesn't DO anything. It just tells the compiler to stop allowing you to generate code that requires the object to be "derived".

bool useDerived = true; // this could be a variable or parameter, etc.
base o1;
derived o2;
base *p1 = (useDerived) ? &o2 : &o1;
count << p1->a;
count << p1->b; // compiler error because compiler isn't allowed to "know" that p1 points to o2.
derived *p2 = dynamic_cast<derived>(p1);
// p2 is null if useDerived is false, otherwise it's &o2
derived *p3 = reinterpret_case<derived(p1);
// p3 = p1 always, without ... but if p1 is NOT a derived object, bad stuff happens when ...
count << p3->b; // if object is o1, then we've just run code to read pseudo-random memory and tried to act like its part of out object. (its probably the address of o2's vtable pointer :)

// now imaging this horror ...
p3->b = 0; // we just nulled out some random memory ... really scary things might happen now.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!