c++ getClass()?

C

cmk128

Hi
How can i do something like Java:

if (str.getClass()==String.class){
}


i found that, this syntax has error in g++:

if (typeof(a) == typeof(b)){
}

thanks
from Peter ([email protected])
 
V

Victor Bazarov

Hi
How can i do something like Java:

if (str.getClass()==String.class){
}


i found that, this syntax has error in g++:

if (typeof(a) == typeof(b)){
}

Try

if (typeid(a) == typeid(b)){

Don't forget to '#include <typeinfo>'

V
 
D

Daniel T.

Hi
How can i do something like Java:

if (str.getClass()==String.class){
}


i found that, this syntax has error in g++:

if (typeof(a) == typeof(b)){
}

class A { public: virtual ~A() { } };
class B : public A { };

int main()
{
A a, b;
assert( typeid( a ) == typeid( b ) );
B c;
assert( typeid( a ) != typeid( c ) );
}

Frankly though, if you need to do the above, then you probably have a design problem.
 
C

cmk128

Daniel T. ¼g¹D¡G
class A { public: virtual ~A() { } };
class B : public A { };

int main()
{
A a, b;
assert( typeid( a ) == typeid( b ) );
B c;
assert( typeid( a ) != typeid( c ) );
}

Frankly though, if you need to do the above, then you probably have a design problem.

thank you so much :)
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top