dynamic_cast and generic pointer

Discussion in 'C++' started by asit, Sep 6, 2011.

  1. asit

    asit Guest

    /*
    * File: main.cpp
    * Author: asit
    *
    * Created on 6 September, 2011, 7:08 PM
    */

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    /*
    *
    */

    class A
    {
    public:
    virtual void show()
    {
    cout<<"A"<<endl;
    }
    };
    class B : public A
    {
    public:
    void show()
    {
    cout<<"B"<<endl;
    }
    };
    class C : public A
    {
    public:
    void show()
    {
    cout<<"C"<<endl;
    }
    };

    int main(int argc, char** argv)
    {
    B ob = new B;
    void *ptr;
    ptr = dynamic_cast<void*>(&ob);
    B* pt2 = dynamic_cast<B*>(ptr);
    return 0;
    }



    In the above code, why does the casting fails ??
     
    asit, Sep 6, 2011
    #1
    1. Advertising

  2. On 9/6/2011 3:38 PM, asit wrote:
    > /*
    > * File: main.cpp
    > * Author: asit
    > *
    > * Created on 6 September, 2011, 7:08 PM
    > */
    >
    > #include<cstdlib>
    > #include<iostream>
    >
    > using namespace std;
    >
    > /*
    > *
    > */
    >
    > class A
    > {
    > public:
    > virtual void show()
    > {
    > cout<<"A"<<endl;
    > }
    > };
    > class B : public A
    > {
    > public:
    > void show()
    > {
    > cout<<"B"<<endl;
    > }
    > };
    > class C : public A
    > {
    > public:
    > void show()
    > {
    > cout<<"C"<<endl;
    > }
    > };
    >
    > int main(int argc, char** argv)
    > {
    > B ob = new B;

    ^^^^^^^^^^^^^ Cannot happen in C++
    > void *ptr;
    > ptr = dynamic_cast<void*>(&ob);
    > B* pt2 = dynamic_cast<B*>(ptr);
    > return 0;
    > }
    >
    >
    >
    > In the above code, why does the casting fails ??


    Because it's not C++ code...

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 6, 2011
    #2
    1. Advertising

  3. asit

    asit Guest

    why it's not c++ code ?
     
    asit, Sep 6, 2011
    #3
  4. asit

    Ian Collins Guest

    On 09/ 7/11 09:23 AM, asit wrote:
    > why it's not c++ code ?


    Why what isn't C++ code?

    The original code you posted wouldn't compile because you can't apply
    dynamic_cast to a void*

    --
    Ian Collins
     
    Ian Collins, Sep 6, 2011
    #4
  5. asit

    Default User Guest

    "asit" <> wrote in message
    news:...
    > why it's not c++ code ?


    Restoring the context:

    > > B ob = new B;

    > ^^^^^^^^^^^^^ Cannot happen in C++


    Because ob is not a pointer to B, which is required if you assign the result
    of new to it.



    Brian
     
    Default User, Sep 7, 2011
    #5
  6. asit

    Asger-P Guest

    Hi asit

    On the: 06. of september-2011 At: 21:38 asit wrote:

    > int main(int argc, char** argv)
    > {
    > B ob = new B;
    > void *ptr;
    > ptr = dynamic_cast<void*>(&ob);
    > B* pt2 = dynamic_cast<B*>(ptr);
    > return 0;
    > }


    That will not compile!

    This is how dynamic_cast usually is used as far as I know.

    int main(int argc, char** argv)
    {
    A *ob = new B;
    B* pt2 = dynamic_cast<B*>(ob);
    if( pt2 )
    cout << "pt2 is of type B" << endl;
    return 0;
    }


    Best regards
    Asger-P
     
    Asger-P, Sep 7, 2011
    #6
  7. asit <> wrote:
    > In the above code, why does the casting fails ??


    Short answer: Because 'void' is not a type with runtime information
    in it.

    Why do you want to use 'void*' in a C++ program anyways?
     
    Juha Nieminen, Sep 7, 2011
    #7
  8. asit

    Goran Guest

    On Sep 7, 8:22 am, Juha Nieminen <> wrote:
    > asit <> wrote:
    > > In the above code, why does the casting fails ??

    >
    >   Short answer: Because 'void' is not a type with runtime information
    > in it.
    >
    >   Why do you want to use 'void*' in a C++ program anyways?


    .... And, even in a C program, void* is, more often that not, a union
    of a given set of pointer types, it's just that the programmer has
    been lazy to realize and write that.

    ;-)

    Goran.
     
    Goran, Sep 7, 2011
    #8
  9. asit

    Joe Greer Guest

    asit <> wrote in news:629a494f-449f-488a-8ac0-
    :

    > why it's not c++ code ?
    >


    Because "new B" returns a B* rather than B. If you want an instance of B,
    you just need

    B ob;

    joe
     
    Joe Greer, Sep 8, 2011
    #9
  10. asit

    BGB Guest

    On 9/7/2011 4:16 AM, Goran wrote:
    > On Sep 7, 8:22 am, Juha Nieminen<> wrote:
    >> asit<> wrote:
    >>> In the above code, why does the casting fails ??

    >>
    >> Short answer: Because 'void' is not a type with runtime information
    >> in it.
    >>
    >> Why do you want to use 'void*' in a C++ program anyways?

    >
    > ... And, even in a C program, void* is, more often that not, a union
    > of a given set of pointer types, it's just that the programmer has
    > been lazy to realize and write that.
    >


    well, far more often, its use is as a pointer type that one can easily
    convert to/from without needing a cast (very useful for things like
    "malloc()" and NULL).

    in my own uses, a 'void*' pointer can be subject to dynamic
    type-checking, but this is partly because my dynamic type checking works
    very differently than C++'s RTTI mechanism (it does not depend on the
    pointed-to object, or even necessarily that the pointer points to
    addressable memory, as many types/values may be encoded directly into
    the pointer itself).

    RTTI, OTOH, generally needs to make use of the object's vtable, which
    granted means it requires an object to work...


    however, I eventually switched to a non-void pointer type, for several
    reasons:
    because the use cases for these pointers was sufficiently
    non-overlapping with standard pointers that one really *does* want the
    compiler to complain about implicit conversions to/from this type;
    technically, if it is something like an incomplete struct type or
    similar, then operator overloading can be used with it.

    actually, IME the compiler only seems to care about a struct type being
    incomplete if one ever tries to access something through it, but since
    there is not really anything "sane" on the other end of this pointer, an
    incomplete struct better expresses intention IMO (serving as a
    placeholder), and also causes the compiler to object if or whenever one
    stupidly tries to use "*obj" or "obj->x" with the type.


    in my conventions, I far more often use the name "dyt" (short for
    "dynamic type") for these "magic" pointers.

    for normal C and C++ code, it is a typedef for "struct _dyt_s *",
    however, this type is handled specially by my tools (and a different
    declaration is used in this case).


    I have yet to devise a system to cleanly interface my mechanism with
    RTTI though.


    > ;-)
    >
    > Goran.
     
    BGB, Sep 9, 2011
    #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. Axter
    Replies:
    3
    Views:
    3,827
    Richard Herring
    May 13, 2005
  2. Ganesh
    Replies:
    3
    Views:
    511
    red floyd
    May 23, 2005
  3. Replies:
    5
    Views:
    641
    Howard
    Jun 27, 2005
  4. verec
    Replies:
    5
    Views:
    3,106
    verec
    Aug 16, 2005
  5. Tomás
    Replies:
    1
    Views:
    339
    Victor Bazarov
    Feb 24, 2006
Loading...

Share This Page