dynamic_cast and generic pointer

A

asit

/*
* 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 ??
 
V

Victor Bazarov

/*
* 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
 
A

Asger-P

Hi asit

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
 
J

Juha Nieminen

asit said:
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?
 
G

Goran

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

BGB

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top