Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9827 > unrolled thread

dynamic_cast and generic pointer

Started byasit <lipun4u@gmail.com>
First post2011-09-06 12:38 -0700
Last post2011-09-08 23:17 -0700
Articles 11 — 10 participants

Back to article view | Back to comp.lang.c++


Contents

  dynamic_cast and generic pointer asit <lipun4u@gmail.com> - 2011-09-06 12:38 -0700
    Re: dynamic_cast and generic pointer Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-06 16:48 -0400
      Re: dynamic_cast and generic pointer asit <lipun4u@gmail.com> - 2011-09-06 14:23 -0700
        Re: dynamic_cast and generic pointer Ian Collins <ian-news@hotmail.com> - 2011-09-07 10:09 +1200
        Re: dynamic_cast and generic pointer "Default User" <defaultuserbr@yahoo.com> - 2011-09-06 18:07 -0500
        Re: dynamic_cast and generic pointer Joe Greer <jgreer@doubletake.com> - 2011-09-08 14:45 +0000
    Re: dynamic_cast and generic pointer Asger-P <junk@asger-p.dk> - 2011-09-07 01:08 +0200
    Re: dynamic_cast and generic pointer Paavo Helde <myfirstname@osa.pri.ee> - 2011-09-07 00:02 -0500
    Re: dynamic_cast and generic pointer Juha Nieminen <nospam@thanks.invalid> - 2011-09-07 06:22 +0000
      Re: dynamic_cast and generic pointer Goran <goran.pusic@gmail.com> - 2011-09-07 04:16 -0700
        Re: dynamic_cast and generic pointer BGB <cr88192@hotmail.com> - 2011-09-08 23:17 -0700

#9827 — dynamic_cast and generic pointer

Fromasit <lipun4u@gmail.com>
Date2011-09-06 12:38 -0700
Subjectdynamic_cast and generic pointer
Message-ID<3d7f54db-fcbe-4f00-92c4-8d63c38d8cab@glegroupsg2000goo.googlegroups.com>
/* 
 * 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 ??

[toc] | [next] | [standalone]


#9829

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-09-06 16:48 -0400
Message-ID<j460vh$jro$1@dont-email.me>
In reply to#9827
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

[toc] | [prev] | [next] | [standalone]


#9834

Fromasit <lipun4u@gmail.com>
Date2011-09-06 14:23 -0700
Message-ID<629a494f-449f-488a-8ac0-dfd2bcf040bc@glegroupsg2000goo.googlegroups.com>
In reply to#9829
why it's not c++ code ?

[toc] | [prev] | [next] | [standalone]


#9836

FromIan Collins <ian-news@hotmail.com>
Date2011-09-07 10:09 +1200
Message-ID<9cnk1lF72aU9@mid.individual.net>
In reply to#9834
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

[toc] | [prev] | [next] | [standalone]


#9849

From"Default User" <defaultuserbr@yahoo.com>
Date2011-09-06 18:07 -0500
Message-ID<9cnneeFfoeU1@mid.individual.net>
In reply to#9834
"asit" <lipun4u@gmail.com> wrote in message 
news:629a494f-449f-488a-8ac0-dfd2bcf040bc@glegroupsg2000goo.googlegroups.com...
> 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

[toc] | [prev] | [next] | [standalone]


#9927

FromJoe Greer <jgreer@doubletake.com>
Date2011-09-08 14:45 +0000
Message-ID<Xns9F5A6D692112Cjgreerdoubletakecom@88.198.244.100>
In reply to#9834
asit <lipun4u@gmail.com> wrote in news:629a494f-449f-488a-8ac0-
dfd2bcf040bc@glegroupsg2000goo.googlegroups.com:

> 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

[toc] | [prev] | [next] | [standalone]


#9850

FromAsger-P <junk@asger-p.dk>
Date2011-09-07 01:08 +0200
Message-ID<op.v1es88gs2juju3@ajwin7>
In reply to#9827
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

[toc] | [prev] | [next] | [standalone]


#9863

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2011-09-07 00:02 -0500
Message-ID<Xns9F5951C7FB08myfirstnameosapriee@216.196.109.131>
In reply to#9827
asit <lipun4u@gmail.com> wrote in news:3d7f54db-fcbe-4f00-92c4-
8d63c38d8cab@glegroupsg2000goo.googlegroups.com:

> class B : public A 
[...]
>     void *ptr;
[...]
>     B* pt2 = dynamic_cast<B*>(ptr);

dynamic_cast works at run-time, typically using vtable pointers in the 
objects; when given a void* it does not know if it points to something 
having a vtable, and where in the object the vtable pointer might be 
located, so there is no way how it could work. 

If you know that the void* pointer had been obtained by casting an A* 
pointer to void*, then you can first static_cast to A* (this round-trip is 
guaranteed to work) and then continue with dynamic_cast:

B x;
A* initial_ptr = &x;
void* ptr = static_cast<void*>(initial_ptr);

B* ptr2 = dynamic_cast<B*>(static_cast<A*>(ptr));
// now ptr2 will point to the original x;

hth
Paavo

[toc] | [prev] | [next] | [standalone]


#9865

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-09-07 06:22 +0000
Message-ID<4e670d94$0$3067$7b1e8fa0@news.nbl.fi>
In reply to#9827
asit <lipun4u@gmail.com> 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?

[toc] | [prev] | [next] | [standalone]


#9890

FromGoran <goran.pusic@gmail.com>
Date2011-09-07 04:16 -0700
Message-ID<6f71b818-9d6a-44ed-a69e-c15048a6465e@d2g2000vby.googlegroups.com>
In reply to#9865
On Sep 7, 8:22 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> asit <lipu...@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#9955

FromBGB <cr88192@hotmail.com>
Date2011-09-08 23:17 -0700
Message-ID<j4cb1m$kb$1@news.albasani.net>
In reply to#9890
On 9/7/2011 4:16 AM, Goran wrote:
> On Sep 7, 8:22 am, Juha Nieminen<nos...@thanks.invalid>  wrote:
>> asit<lipu...@gmail.com>  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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web