Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If we have a base class BASE and derived class DERIVED (which derives from BASE class), then what does following statement would be treated as means whether it is upcasting or down-casting not a cast.

C++
BASE* pb = new DERIVED();


Here what I think is that if we have a virtual function in base class and same function is overridden in derived class then when we call that function using pb it will call derived class function.in this case this statement treats as downcasting.
On the other hand, If we have a nonvirtual function in BASE class and we call that function with pb then it would call base class function. then above statement treats as up-casting.
So please clear my doubt or is it like that it is not a casting?

Thanks in advance.
Posted
Updated 8-Aug-13 21:31pm
v2

1 solution

We are talking about casting only when converting the pointer or reference, calling a method on that pointer or reference has nothing to do with a cast. This statement is an assignment and an upcast. When you call a virtual method on the base class pointer then the override method of the derived class is executed through "dynamic dispatch", of course only if the actually created instance has a "more derived version" of the virtual function otherwise the base class virtual function gets executed. In case of nonvirtual method call there is nothing to explain...

The trick what happens in case of virtual function call: You cast the pointer to a base class pointer but objects having virtual methods also have a hidden internal "vtable" (virtual method table) that is initialized when you create the object. Since you created an instance of the DERIVED class, the vtable will point to the virtual method table of the DERIVED class even if you cast the pointer to a BASE pointer (and because of the vtable of the derived class the object will know from itself that its a DERIVED instance even if you access it through a base class pointer). By casting the pointer to a base class pointer you are accessing the very same virtual methods that you could access with a DERIVED pointer (if we overlook covariant return types that are rarely used and mostly unknown for programmers?), the upcast has effect only on the visibility of virtual/nonvirtual methods and member variables. With a base class pointer you simple don't see the member variables and nonvirtual methods defined in the derived class (this is quite obvious). It is also possible to increase the visibility of Base class members in derived classes (for example from protected to public, even in case of virtual methods) but this is rarely used in c++.
 
Share this answer
 
v5
Comments
nv3 9-Aug-13 3:59am    
Nicely explained.
pasztorpisti 9-Aug-13 4:06am    
Thank you!
Rahul from Poona 9-Aug-13 6:34am    
@pasztorpisti
One doubt: Upcasting means we extract BASE part of the object which is derived but here the pointer is pointing to whole DERIVED class object.
How can it be an upcast.
May be possible that my understanding is wrong.
If it is wrong then please can u tell me what upcast and downcast exactly do.
pasztorpisti 9-Aug-13 7:13am    
Upcasting is converting a derived pointer to one of its base class pointers. Upcast is safe and automatic. Downcast is casting a base class pointer to a derived class pointer. Downcast is unsafe and you have to explicitly tell the compiler to do it like static_cast<derived*>(base_ptr_variable);

To find out what they do read a book about object oriented programming and the purpose of polymorphysm. Withtout knowing these concepts you simply wont be able to understand why is it good to upcast something. On the other hand if your program contains downcasts then you probably made serious design mistakes.
H.Brydon 9-Aug-13 10:54am    
Perfect answer, +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900