Understanding Typecasting in C++

Discussion in 'C++' started by Kapil Khosla, Jul 19, 2003.

  1. Kapil Khosla

    Kapil Khosla Guest

    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();


    }
     
    Kapil Khosla, Jul 19, 2003
    #1
    1. Advertising

  2. "Kapil Khosla" <> 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
     
    Victor Bazarov, Jul 19, 2003
    #2
    1. Advertising

  3. "Kapil Khosla" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jul 20, 2003
    #3
  4. "Rolf Magnus" <> wrote in message
    news:bfds3q$64v$05$-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
     
    John Harrison, Jul 20, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Smlt?=

    ArrayList typecasting from binary SQL data

    =?Utf-8?B?Smlt?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    528
    Bruce Barker
    Apr 11, 2005
  2. Nicolay Korslund
    Replies:
    7
    Views:
    484
    Nicolay Korslund
    Sep 30, 2003
  3. venkatesh
    Replies:
    1
    Views:
    8,212
    lallous
    Dec 6, 2003
  4. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,963
    Robert Street
    Feb 21, 2004
  5. cman
    Replies:
    3
    Views:
    1,636
Loading...

Share This Page