dynamic cast vs reinterpret_cast

Discussion in 'C++' started by Asif Zaidi, Feb 9, 2010.

  1. Asif Zaidi

    Asif Zaidi Guest

    Hi:
    I have the 2 classes below. I am trying to cast them such that output
    should be different. But I am getting to be the same all the time.
    Suggestions only plz. Thanks

    class Base { public: virtual void foo() {} };
    class Derived : public Base { public: virtual void foo() { cout << "
    in derived" << endl; } };

    int main()
    {
    Derived dobj;
    Base *b = &dobj;

    cout << dynamic_cast<Derived*>(b) << endl;
    cout << reinterpret_cast<Derived*>(b) << endl;
    }
     
    Asif Zaidi, Feb 9, 2010
    #1
    1. Advertising

  2. Asif Zaidi

    Asif Zaidi Guest

    On Feb 9, 2:33 am, Asif Zaidi <> wrote:
    > Hi:
    > I have the 2 classes below. I am trying to cast them such that output
    > should be different. But I am getting to be the same all the time.
    > Suggestions only plz. Thanks
    >
    > class Base { public:    virtual void foo() {}  };
    > class Derived : public Base { public:   virtual void foo() { cout << "
    > in derived" << endl; } };
    >
    > int main()
    > {
    >         Derived dobj;
    >         Base *b = &dobj;
    >
    >         cout << dynamic_cast<Derived*>(b)     << endl;
    >         cout << reinterpret_cast<Derived*>(b) << endl;
    >
    > }
    >
    >


    No need to respond plz... figured it out

    Thanks

    Asif
     
    Asif Zaidi, Feb 9, 2010
    #2
    1. Advertising

  3. Asif Zaidi wrote:
    > On Feb 9, 2:33 am, Asif Zaidi <> wrote:
    >> Hi:
    >> I have the 2 classes below. I am trying to cast them such that output
    >> should be different. But I am getting to be the same all the time.
    >> Suggestions only plz. Thanks
    >>
    >> class Base { public: virtual void foo() {} };
    >> class Derived : public Base { public: virtual void foo() { cout << "
    >> in derived" << endl; } };
    >>
    >> int main()
    >> {
    >> Derived dobj;
    >> Base *b = &dobj;
    >>
    >> cout << dynamic_cast<Derived*>(b) << endl;
    >> cout << reinterpret_cast<Derived*>(b) << endl;
    >>
    >> }
    >>
    >>

    >
    > No need to respond plz... figured it out


    Sorry for responding, but I am really wondering what you figured out.
    The code you posted gives lots of compiling errors.

    1) Doing the reinterpret_cast from the base class to the derived class
    makes no sense (is it working at all?).
    2) Both classes have no stream operator defined, so how can you pass
    them to cout?
     
    Vladimir Jovic, Feb 9, 2010
    #3
  4. Asif Zaidi

    Asif Zaidi Guest

    On Feb 9, 3:52 am, Vladimir Jovic <> wrote:
    > Asif Zaidi wrote:
    > > On Feb 9, 2:33 am, Asif Zaidi <> wrote:
    > >> Hi:
    > >> I have the 2 classes below. I am trying to cast them such that output
    > >> should be different. But I am getting to be the same all the time.
    > >> Suggestions only plz. Thanks

    >
    > >> class Base { public:    virtual void foo() {}  };
    > >> class Derived : public Base { public:   virtual void foo() { cout << "
    > >> in derived" << endl; } };

    >
    > >> int main()
    > >> {
    > >>         Derived dobj;
    > >>         Base *b = &dobj;

    >
    > >>         cout << dynamic_cast<Derived*>(b)     << endl;
    > >>         cout << reinterpret_cast<Derived*>(b) << endl;

    >
    > >> }

    >
    > > No need to respond plz... figured it out

    >
    > Sorry for responding, but I am really wondering what you figured out.
    > The code you posted gives lots of compiling errors.
    >
    > 1) Doing the reinterpret_cast from the base class to the derived class
    > makes no sense (is it working at all?).
    > 2) Both classes have no stream operator defined, so how can you pass
    > them to cout?


    Hi

    1) Yes.. I wanted to get a different address but am getting the same
    address with above code. I thought i had it figured out but apparently
    not.
    2) I am just printing out the address that it is pointing to.

    I am using a Microsoft Visual 2008 C++ compiler. The code did compile
    and run and i Saw the output.

    Thanks

    Asif
     
    Asif Zaidi, Feb 9, 2010
    #4
  5. Asif Zaidi wrote:
    > On Feb 9, 3:52 am, Vladimir Jovic <> wrote:
    >> Asif Zaidi wrote:
    >>> [..]

    >> Sorry for responding, but I am really wondering what you figured out.
    >> The code you posted gives lots of compiling errors.
    >>
    >> [..]

    >
    > I am using a Microsoft Visual 2008 C++ compiler. The code did compile
    > and run and i Saw the output.


    Just a few words of caution: make sure when you set up your VC++
    projects for the purposes of learning C++, that you disable language
    extensions (a setting somewhere among C++ Language options). That will
    likely prevent you from building Windows executables (Windows SDK
    headers are full of code requiring the extensions), but at least you (a)
    can build Console exes, and (b) you will be much closer to the real
    language than to the MS dialect of it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 9, 2010
    #5
  6. * Asif Zaidi:
    >> >> class Base { public: ? ?virtual void foo() {} ?};
    >> >> class Derived : public Base { public: ? virtual void foo() { cout << "
    >> >> in derived" << endl; } };


    [snip]

    > 1) Yes.. I wanted to get a different address but am getting the same
    > address with above code. I thought i had it figured out but apparently
    > not.


    You won't. Why would you? There's no reason to.

    Now, if you were using multiple inheritance, *then* you'd see a difference
    (And then the reinterpret_cast<> or a C-stype cast will no longer work)

    --
    Martijn van Buul -
     
    Martijn van Buul, Feb 9, 2010
    #6
  7. Asif Zaidi

    Noah Roberts Guest

    In article <>, says...
    >
    > * Asif Zaidi:
    > >> >> class Base { public: ? ?virtual void foo() {} ?};
    > >> >> class Derived : public Base { public: ? virtual void foo() { cout << "
    > >> >> in derived" << endl; } };

    >
    > [snip]
    >
    > > 1) Yes.. I wanted to get a different address but am getting the same
    > > address with above code. I thought i had it figured out but apparently
    > > not.

    >
    > You won't. Why would you? There's no reason to.
    >
    > Now, if you were using multiple inheritance, *then* you'd see a difference
    > (And then the reinterpret_cast<> or a C-stype cast will no longer work)


    The C-style cast would still work because it would, knowing the types of
    both pointers, use a static cast instead of a reinterpret cast. Thus,
    so long as you're not attempting to cross-cast (don't believe c-style
    casts ever resolve to dynamic cast) it will work.

    If, on the other hand, we had a main something like so:

    int main()
    {
    struct X;
    struct Y;
    X* get_x();

    std::cout << (Y*)(get_x()) << std::endl;
    }

    assuming X and Y are sub/base related, the above c-style would do a
    reinterpret cast because it doesn't know that static is possible, nor
    how to do it. With the full definition of X and Y though it would do a
    static.

    That is, in my opinion, one of the strongest arguments against using c-
    style casts. C-style casts are different casts depending on context and
    it can get pretty hairy knowing what's what.

    --
    http://crazyeddiecpp.blogspot.com
     
    Noah Roberts, Feb 9, 2010
    #7
  8. Asif Zaidi

    James Kanze Guest

    On 9 Feb, 16:06, Juha Nieminen <> wrote:
    > Vladimir Jovic wrote:
    > > 2) Both classes have no stream operator defined, so how can you pass
    > > them to cout?


    > If you give a pointer to cout, it will print the value of that
    > pointer (ie usually the memory address it's pointing to).


    Unless it's a pointer to a character type:).

    --
    James Kanze
     
    James Kanze, Feb 9, 2010
    #8
  9. Juha Nieminen wrote:
    > James Kanze wrote:
    >> On 9 Feb, 16:06, Juha Nieminen <> wrote:
    >>> Vladimir Jovic wrote:
    >>>> 2) Both classes have no stream operator defined, so how can you pass
    >>>> them to cout?
    >>> If you give a pointer to cout, it will print the value of that
    >>> pointer (ie usually the memory address it's pointing to).

    >> Unless it's a pointer to a character type:).

    >
    > Assume you wanted to output the value of a char* rather than the
    > string it's pointing to. What would be the "proper" way of doing that?


    static_cast<void*>, probably.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 10, 2010
    #9
  10. Asif Zaidi

    James Kanze Guest

    On Feb 10, 7:54 pm, Juha Nieminen <> wrote:
    > James Kanze wrote:
    > > On 9 Feb, 16:06, Juha Nieminen <> wrote:
    > >> Vladimir Jovic wrote:
    > >>> 2) Both classes have no stream operator defined, so how can you pass
    > >>> them to cout?


    > >> If you give a pointer to cout, it will print the value of
    > >> that pointer (ie usually the memory address it's pointing
    > >> to).


    > > Unless it's a pointer to a character type:).


    > Assume you wanted to output the value of a char* rather than
    > the string it's pointing to. What would be the "proper" way of
    > doing that?


    Cast it to void*.

    --
    James Kanze
     
    James Kanze, Feb 10, 2010
    #10
    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. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,079
    Dan Pop
    Feb 10, 2004
  2. EvilRix
    Replies:
    8
    Views:
    648
    Martin Dickopp
    Feb 14, 2004
  3. Kobe
    Replies:
    3
    Views:
    578
    Tomás
    Feb 15, 2006
  4. Pavel
    Replies:
    7
    Views:
    535
    Pavel
    Sep 19, 2010
  5. Alex Vinokur
    Replies:
    1
    Views:
    580
Loading...

Share This Page