can I dynamically convert a base class object into a derived class object?
-
e.g:
class DerivedClass:BaseClass
{
void functionInDerivedClassOnly();
}
BaseClass pBaseClass = new BaseClass();
//can the following line be excuted correctly?
(DerivedClass*)pBaseClass->functionInDerivedClassOnly();
delete pBaseClass;Thank you very much!!! ------------------- I am learning C++ and English
-
e.g:
class DerivedClass:BaseClass
{
void functionInDerivedClassOnly();
}
BaseClass pBaseClass = new BaseClass();
//can the following line be excuted correctly?
(DerivedClass*)pBaseClass->functionInDerivedClassOnly();
delete pBaseClass;Thank you very much!!! ------------------- I am learning C++ and English
In general no. In the code above the compiler will go ahead and treat
pBaseClass
as aDerivedClass
but unless it really is one you're asking for trouble. Steve -
In general no. In the code above the compiler will go ahead and treat
pBaseClass
as aDerivedClass
but unless it really is one you're asking for trouble. Steveyour answer is similar with my guess. but is there any way to implement the dynamical conversion from the base class object to a derived one. if not ,I would have to delete the base class object and recreate a derived one with the
new
operator. in this case I can't see the benefit of the inheritance, can I? Thank you very much!!! ------------------- I am learning C++ and English -
your answer is similar with my guess. but is there any way to implement the dynamical conversion from the base class object to a derived one. if not ,I would have to delete the base class object and recreate a derived one with the
new
operator. in this case I can't see the benefit of the inheritance, can I? Thank you very much!!! ------------------- I am learning C++ and EnglishYou could write a constructor in the derived type that takes a reference to the base type. Why do you need to do this? It is not something that one finds he has to do often in OO code. Typically the situation is that you create a derived type but talk to it using a base class interface. Steve
-
e.g:
class DerivedClass:BaseClass
{
void functionInDerivedClassOnly();
}
BaseClass pBaseClass = new BaseClass();
//can the following line be excuted correctly?
(DerivedClass*)pBaseClass->functionInDerivedClassOnly();
delete pBaseClass;Thank you very much!!! ------------------- I am learning C++ and English
This will help you: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_72.asp[^]
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
In general no. In the code above the compiler will go ahead and treat
pBaseClass
as aDerivedClass
but unless it really is one you're asking for trouble. SteveThank you guys, I finally understand it with your help. Only if it "is" actually the "specified type" in the memory can you convert it into the "specified type" . I have thought through and figure that maybe my problem dose be a complicated one and I can't find a short cut for it . Thank you very much, again!!! ------------------- I am learning C++ and English
-
e.g:
class DerivedClass:BaseClass
{
void functionInDerivedClassOnly();
}
BaseClass pBaseClass = new BaseClass();
//can the following line be excuted correctly?
(DerivedClass*)pBaseClass->functionInDerivedClassOnly();
delete pBaseClass;Thank you very much!!! ------------------- I am learning C++ and English
ewighell wrote:
BaseClass pBaseClass = new BaseClass();
replace with
BaseClass pBaseClass = new DerivedClass();
that's what polymorphism was designed for !
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -- modified at 3:46 Thursday 19th January, 2006 -
In general no. In the code above the compiler will go ahead and treat
pBaseClass
as aDerivedClass
but unless it really is one you're asking for trouble. SteveScore: 1.8 (2 votes).
How are the scores calculated? Obviously not an average as 3.6 (2x1.8) can't be obtained by adding 2 scores. Steve
-
your answer is similar with my guess. but is there any way to implement the dynamical conversion from the base class object to a derived one. if not ,I would have to delete the base class object and recreate a derived one with the
new
operator. in this case I can't see the benefit of the inheritance, can I? Thank you very much!!! ------------------- I am learning C++ and EnglishMaybe we can help you if you describe clearly what you need to do
-
e.g:
class DerivedClass:BaseClass
{
void functionInDerivedClassOnly();
}
BaseClass pBaseClass = new BaseClass();
//can the following line be excuted correctly?
(DerivedClass*)pBaseClass->functionInDerivedClassOnly();
delete pBaseClass;Thank you very much!!! ------------------- I am learning C++ and English
just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class. Its just like calling Donkey a dog does not make donkey to bark ;). so you can do this.
BaseClass* pBaseClass = new DerivedClass(); ((DerivedClass*)pBaseClass)->functionInDerivedClassOnly();
or usereinterpret_cast
-Prakash
-
just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class. Its just like calling Donkey a dog does not make donkey to bark ;). so you can do this.
BaseClass* pBaseClass = new DerivedClass(); ((DerivedClass*)pBaseClass)->functionInDerivedClassOnly();
or usereinterpret_cast
-Prakash
hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
Score: 1.8 (2 votes).
How are the scores calculated? Obviously not an average as 3.6 (2x1.8) can't be obtained by adding 2 scores. Steve
-
hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...]He reinterpreted it.;P
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
-
He reinterpreted it.;P
Jesus Loves:rose:
--Owner Drawn:rose: --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord:rose:
Owner drawn wrote:
He reinterpreted it
without quoting or refering to me ?! ;P not that sure...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
just by casting the baseclass pointer to derived class pointer does not make the object that was created i.e. baseclass into a dereived class. Its just like calling Donkey a dog does not make donkey to bark ;). so you can do this.
BaseClass* pBaseClass = new DerivedClass(); ((DerivedClass*)pBaseClass)->functionInDerivedClassOnly();
or usereinterpret_cast
-Prakash
In the example above you should use
static_cast<DerivedClass*>(pBaseClass)
. It's safer (but still not 100% safe) as it will only compile if it's possible forBaseClass
s to exist which are alsoDerivedClass
s (there is an inheritance relationship between them). Steve -
hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...]toxcct wrote:
hey, you posted the same thing as i did 1 hour later
I did not read the other solutions ;)
-Prakash
-
In the example above you should use
static_cast<DerivedClass*>(pBaseClass)
. It's safer (but still not 100% safe) as it will only compile if it's possible forBaseClass
s to exist which are alsoDerivedClass
s (there is an inheritance relationship between them). SteveStephen Hewitt wrote:
In the example above you should use static_cast(pBaseClass)
yes, correct. Thanks.
-Prakash
-
hey, you posted the same thing as i did 1 hour later :sigh: where were you ? :|
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
Score: 1.8 (2 votes).
How are the scores calculated? Obviously not an average as 3.6 (2x1.8) can't be obtained by adding 2 scores. Steve
the scores are ponderated with the level of the members... - a bronze member votes for 1 voice - a silver member votes for 2 voices - a gold member votes for 4 voices - a platinum member votes for 8 voices
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
toxcct wrote:
where were you ?
Me, Sleeping :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
:zzz::zzz: zzzZZZZZZZzzzzz SShhhhhhh :zzz::zzz:
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...]