Understanding Typecasting in C++

K

Kapil Khosla

Hi,
I have been trying to understand this concept for quite sometime now
somehow I am missing some vital point. I am new to Object Oriented
Programming so maybe thats the reason.

I want to understand what is typecasting in C++. Say I have a Base
class and a Derived class, I have a pointer to an object to each.
Base *ba = new Base;
Derived *de = new Derived;

Now when I do something like
Base *one = (Base*)de;

What is really happening here ? "de" was an object of class Derived
and had data specific to that class. Now by typecasting I have access
to all the functions and data of the Base class but where is the data
of my Derived class object , I no longer have access to it, if I do
one-> ....(derived class data)

Thanks,
Kapil

#include "stdafx.h"
// Using pragmas to see the difference in programs in binary level

using namespace std;


class Base
{
public:
int a,b;
void print();
};

class Derived : public Base
{

public:
int c,d;
void print();
};


void Base::print()
{
cout << "In Base" << endl;
}


void Derived::print()
{
cout <<"In Derived" << endl;
}

int main()
{

Base ba;
Derived de;
Base *a = new Base; // a is a pointer to the Base object
Derived *b = new Derived; // b is the pointer to the Derived object


Base *c = (Base*)b;
c->print();

Derived *d = (Derived*)b;
d->print();


}
 
V

Victor Bazarov

Kapil Khosla said:
I have been trying to understand this concept for quite sometime now
somehow I am missing some vital point. I am new to Object Oriented
Programming so maybe thats the reason.

I want to understand what is typecasting in C++. Say I have a Base
class and a Derived class, I have a pointer to an object to each.

Presumably, 'Derived' is actually derived from 'Base', right?
Base *ba = new Base;
Derived *de = new Derived;

Now when I do something like
Base *one = (Base*)de;

No cast is necessary. Conversion from a derived to its base
is implicit. You may write

Base *one = de;
What is really happening here ? "de" was an object of class Derived
and had data specific to that class.

Well, 'de' wasn't an object. 'de' is a pointer to an object.
Now by typecasting I have access
to all the functions and data of the Base class but where is the data
of my Derived class object , I no longer have access to it, if I do
one-> ....(derived class data)

What happens when you convert a pointer to a derived class to
a pointer to a base class, the compiler computes the location
of the base subobject in the derived object and returns the
address of that subobject.

de ---> +------------------+
| Derived object |
one ---> +------------+ |
| | Base | |
| | subobject | |
| +------------+ |
| |
+------------------+

And, no, you don't have access to 'de's data through 'one'
simply because they are different objects.

Victor
 
J

John Harrison

Kapil Khosla said:
Hi,
I have been trying to understand this concept for quite sometime now
somehow I am missing some vital point. I am new to Object Oriented
Programming so maybe thats the reason.

I want to understand what is typecasting in C++. Say I have a Base
class and a Derived class, I have a pointer to an object to each.
Base *ba = new Base;
Derived *de = new Derived;

Now when I do something like
Base *one = (Base*)de;

This cast is not needed. In fact it bad style to cast here. All you need is
this

Base *one = de;
What is really happening here ?

Nothing. I mean that, nothing is happening. You are assigning one pointer to
another, that is all.
"de" was an object of class Derived
and had data specific to that class. Now by typecasting I have access
to all the functions and data of the Base class

No. You had access to those already.
but where is the data
of my Derived class object , I no longer have access to it, if I do
one-> ....(derived class data)

You cannot access the derived data using ba, its still there however and you
can access it though de. ba and de are pointing to the same object, but be
only 'sees' the Base part of it. You can still access the Derived object
through the Base pointer using virtual functions however.
Thanks,
Kapil

You clearly have a long way to go before understanding casting. usually when
people get stuck like this it is because they think things are more
complicated than they are. Its really very simple.

class Animal
{
};

class Monkey : public Animal
{
};

All Monkeys are Animals. So it must be legal to a Monkey pointer to an
Animal pointer. This can be done without a cast.

Monkey m;
Monkey* mp = &m;
Animal* ap = mp; // this is OK, no cast necessary

Now both mp and ap are pointing to (the same) monkey.

But only SOME animals are monkey, so to assigning an animal pointer to a
monkey, is not safe. Because of this if you want to convert an Animal
pointer to a Monkey pointer, you must use a cast.

Monkey m;
Animal* ap = &m; // no cast necessary
Monkey* mp = (Animal*)ap; // cast necessary

Again, both ap and mp are now pointing at the same monkey.

Any more questions please ask. You're clearly misunderstanding something,
but it hard for us to tell exactly what.

john
 
J

John Harrison

Rolf Magnus said:
ITYM:

Monkey* mp = (Monkey*)ap; // cast necessary

Yes of course, thanks for the correction.
But please don't use C style casts. Use the newer C++ casts, in this
case static_cast or dynamic_cast. C style casts don't have the
fine-grained control over what exactly is done that the C++ casts have,
which makes them more error-prone (e.g. casting away const by
accident). In the above example, you can either write:

Monkey* mp = static_cast<Monkey*>(ap);

This does (in this case) the same as the above C style cast, but you
should only write this if you are _absolutely_ sure that ap actually
points to a Monkey, since there is no way to check if the resulting
pointer is valid (and it only is if the object pointed to by ap
actually is a Monkey). For dynamic type checking, use a dynamic_cast:

Monkey* mp = dynamic_cast<Monkey*>(ap);

The resulting pointer will either point to the object, or, if that
object isn't a Monkey, it will be a null pointer.
Note that dynamic_cast only works if your base class is polymorphic,
i.e. it has at least one virtual member function.

All true, but the OP posted his question using C style casts, and since he's
clearly having trouble with those, I thought one concept at a time would be
a better approach.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top