string to class type using typeinfo

C

CT

Hi

I have a function to test if a pointer* is of type string*. I would like
to use dynamic casting to figure it out. How can i convert a string to a
type to useit in the dynamic_cast<"string"> function. His this possible
in c,c++ ?

this function is used when i deserilaize an object, i only keep the
class name. validation purposes.

the first argument is an interface on which all passed object must
implement.

bool VerifyPointer( baseClass *pData, const char * pszClass )
{
return dynamic_cast<pszClass*>( (baseClass *) pData) ? true : false;

// replace pszclass by macro or whatever to convert a string to
type

}

thanks,
 
V

Victor Bazarov

CT said:
I have a function to test if a pointer* is of type string*. I would like
to use dynamic casting to figure it out. How can i convert a string to a
type to useit in the dynamic_cast<"string"> function. His this possible
in c,c++ ?

I don't think there is such thing as 'string' type in C, and I know for
sure there is no such thing as 'dynamic_cast' in C. So, let's drop 'C'
for now.

Now, what do you mean by "pointer*"? Do you mean "void*"? If so, no, it
is not possible to find out what pointer _was_ there before you converted
it to a pointer to void. To convert back you use 'static_cast' and
provide the actual type yourself.
this function is used when i deserilaize an object, i only keep the
class name. validation purposes.

the first argument is an interface on which all passed object must
implement.

bool VerifyPointer( baseClass *pData, const char * pszClass )
{
return dynamic_cast<pszClass*>( (baseClass *) pData) ? true : false;

(a) 'pData' is already of type 'baseClass*', no need to cast it again.

(b) " true : false " looks really strange. Why not just drop the ternary
operator altogether?

(c) There is no conversion of a string to type. Not in C++, anyway. What
you need is probably a big if/else statement to perform different
casts based on the contents of 'pszClass' string. Something like

if (strcmp(pszClass, "string") == 0)
return dynamic_cast<derivedClass_string*>(pData);
else if (strcmp(pszClass, "somethingelse") == 0)
// replace pszclass by macro or whatever to convert a string to
type

}

V
 
R

roberts.noah

CT said:
Hi

I have a function to test if a pointer* is of type string*. I would like
to use dynamic casting to figure it out. How can i convert a string to a
type to useit in the dynamic_cast<"string"> function. His this possible
in c,c++ ?

Since templates are compile time things this wouldn't serve much
purpose; least it seems so to me (someone might find a reason).
However, there is the boost lexical_cast, which can attempt something
like lexical_cast<type>("string")...in other words attempts to parse a
runtime string and turn it into the compile time hardcoded 'type'.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top