If I want to make a method to return an object's type, what type should the method return? A string, the object itself? Any suggestions?

to clarify my q. I want to be able to pass an object as a parameter to the method and the method should return the object's type.

System::Type^ note: you do not need a method to do this; use System::Object::GetType() instead.
eg. System::Type^ type = obj->GetType() ;

I tried that but the compiler did not accept System. What library do I need to include? (I am sorry, I am sooooo new to this)

are you on ISO C++ or C++/CLI (the .net variety)?

i'm sorry; i was under the impression that you were on C++/CLI
in standard C++, the type of an expression is given by the typeid operator which yields
a const std::type_info& eg.

#include <typeinfo>

template< typename T >inline 
const std::type_info& get_type( T& object )
{
  return typeid(object) ;
}
template< typename T >inline 
const std::type_info& get_type( const T& object )
{
  return typeid(object) ;
}

can I use it like this:

const std::type_info& get_type( const MyClass* object )
{
return typeid(object) ;
}


And to call it
obj->get_type(myObj);

what is:
template< typename T >inline

can I use it like this:
const std::type_info& get_type( const MyClass* object )
{
return typeid(object) ;
}

And to call it
obj->get_type(myObj);

yes, you can. remember though that the type of the variable object is a pointer and this would give you the type of the pointer. if you want the type of the object pointed to,

const std::type_info& get_type( const MyClass* object )
{
  return typeid( *object ) ;
}

> what is: template< typename T >inline
if you can not make any sense of that, ignore it for now.
you will come to templates soon enough.

Last q, I promise. I almost got it to work but I get:
error: passing `const TE::MyClass as `this' argument of `const std::type_info& TE::MyClass::get_type(const TE::MyClass*)' discards qualifiers

discards qualifiers has to do with constants, I have read. However I cannot see where the problem is.

const TE::MyClass *obj= new MyClass(string);
obj->get_type(obj); //try to get the type of my obj.
obj is a constant, why, oh why do I get this error?

SORRY! The error should be:
error: passing `const TE::MyClass' as `this' argument of `const std::type_info& TE::MyClass::get_type(const TE::MyClass*)' discards qualifiers

Sorry, again

add a const specifier (in both the declaration and the definition of

const std::type_info& TE::MyClass::get_type(const TE::MyClass*) const ;

and you should probably write

/*const*/ TE::MyClass *obj= new MyClass(string);

or delete obj ; will not work.


why you need this function is beyond me:

const TE::MyClass *obj= new MyClass(string);
obj->get_type(obj); //try to get the type of my obj.

you know at this point the precise type of obj; a pointer to a TE::MyClass object.

=) I have another class that inherits MyClass and I want to be able to check if an object is of the base or deried class. It works beautifully!
I am eternally greateful, many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.