Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Understanding Typecasting in C++

Reply
Thread Tools

Understanding Typecasting in C++

 
 
Kapil Khosla
Guest
Posts: n/a
 
      07-19-2003
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:rint()
{
cout << "In Base" << endl;
}


void Derived:rint()
{
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();


}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-19-2003
"Kapil Khosla" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"Kapil Khosla" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bfds3q$64v$05$(E-Mail Removed)-online.com...
> John Harrison wrote:
>
> > 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

>
> ITYM:
>
> Monkey* mp = (Monkey*)ap; // cast necessary
>


Yes of course, thanks for the correction.

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

>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList typecasting from binary SQL data =?Utf-8?B?Smlt?= ASP .Net 1 04-11-2005 04:49 PM
typecasting the base class to derived class. cman C++ 3 02-23-2004 03:54 AM
Advanced pointer typecasting Robert Street C++ 3 02-21-2004 01:33 AM
Typecasting char pointer to structure venkatesh C++ 1 12-06-2003 06:46 AM
Typecasting operator on simple types vs. classes Nicolay Korslund C++ 7 09-30-2003 04:33 PM



Advertisments