I would just like to understand more about the effects of upcasting.. lets say we have the following classes...
class base
{
public:
foo();
};
class child : public base
{
public:
foo();
foo2();
};
Now, downcasting would be legal and nothing crazy will happen, for example..
child* mychild = new child();
base* mybase = (base*)child;
mybase->foo() //legal
but with direct upcasting, the memory does not align correctly, so it is illegal. Example of upcasting..
base* mybase = new base();
child* mychild = (child*)mybase;
mybase->foo2() //illegal
finally a safe upcast would be...
base* mybase = new child();
child* mychild = (child*)mybase;
mychild->foo2() //legal
My question regards to the final type of cast (the safe upcast). What really happens behind the scene with the cast? Does the memory try to align itself (like float/int)? Does it do any data miniuplation at compile time? Or is it just a gimmick at run-time to tell the compiler what methods and members are allowed to be accessed?
I'm very confused at the overhead involved in doing an safe upcast. If it plays with memory, I'm sure it'll be kinda slow if its done many times in a tight loop. If it doesn't play with memory, would it be safe to say that the upcast has no overhead?
thanks in advace!
Edited by - dynamicman on October 16, 2001 11:26:41 PM