Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1. Can you assign the address of an object of a derived class to a pointer to the base class?

2. Can you assign the address of an object of a base class to a pointer to the derived class?

What I have tried:

I tried reading a book full of errors and confusing explanations.
Posted
Updated 26-Mar-16 0:04am
Comments
Sergey Alexandrovich Kryukov 25-Mar-16 1:27am    
Use a book free of errors and full of comprehensive explanations... :-)
—SA
Philippe Mori 25-Mar-16 10:47am    
How can you know that the book is full of errors if you don't even yet know basic things about the langage.

It's actually easy to understand if you imagine what exactly happens.

First of all, you have to understand that there are compile-time and runtime types. A variable is declared as some base type, but it may reference an object of a derived type during runtime.

Why not visa versa? It's easy. Because derived types are extended, not reduced. Say, some base class "Base" has members A and B. A derived class "Derived" adds some member C, so it has A, B and C.

If your variable is of the compile-time type (is declared as) "Base", you are allowed to access A and B. When you reference "Derived", it also has C, but it still has A and B, so you are safe.

Imagine that you have the variable declared as "Derived", but you managed to assign an instance of "Base" to it. (Here, I don't make difference between assignment of pointers or instances themselves, the idea is the same.) Then the compiler can access not only A and B, but also C; and C does not exist during runtime. Imagine what can happen: you can access memory beyond the object.

The whole idea of OOP is based on working with derived classed referenced by variable of some base classes. To achieve polymorphism, virtual function mechanism is used. But in rare case we may need to violate "pure" OOP a bit, by having a base-type variable and interpreting the referenced object as an object of some derived type. Apparently, this is not always possible, so you first need to make sure that the actual runtime object is compatible with the type you want to interpret is as. (If not, you just skip the operations.) For this purpose, C++ has such a feature as dynamic_cast: Type conversions — C++ Tutorials — dynamic cast[^].

This is also called down-casting. I want to re-iterate: it should not be used on regular basis. Often, it's used as fix to some OOP design which initially did not take into account some requirements, that is, is incorrect, but it's not feasible to redesign it. There are other cases, but they should not be taken as regular technique.

—SA
 
Share this answer
 
v3
Comments
CPallini 25-Mar-16 3:42am    
5.
Sergey Alexandrovich Kryukov 25-Mar-16 9:51am    
Thank you, Carlo.
—SA
[no name] 26-Mar-16 13:04pm    
A, B, C, 5
Sergey Alexandrovich Kryukov 26-Mar-16 13:26pm    
Thank you, Bruno.
—SA
Sergey's answer is correct, but not really easy to read for a beginner. Here is the short version to your question:

(1) YES - Because a derived object can always be treated as its base class

(2) Only if the object that the pointer points to is indeed of the derived class. As the compiler can in general not determine that at compile time, you must explicitly cast the pointer to the derived object. That is called "down casting".

You have three options to make sure that the object is indeed of the derived class you want to cast to:

(a) You as programmer know that under any circumstance it will be such an object. (Hopefully you are correct. If not, your program is likely to crash.)

(b) You store the type of object explicitly in a member of the base class (not very elegant and not really OOP-like)

(c) You let the compiler store runtime information about your objects (enable "runtime type information" in your compiler options). In that case you can use a dynamic cast, which will return a NULL-pointer if the object is not of the expected type, for example:
C++
BaseClass* pBase = ....;
DerivedClass* pDerived = dynamic_cast<derivedclass*> (pBase);
if (pDerived != 0)
{
   // yes - pBase was really pointing to a DerivedClass object
   // an now we can use pDerived to access the derived class's members
   .....
}
 
Share this answer
 
Comments
[no name] 26-Mar-16 13:04pm    
Also a 5 here.
nv3 26-Mar-16 13:42pm    
Danke!

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