473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

string to class type using typeinfo

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,
Jan 18 '06 #1
3 3217

CT wrote:
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'.

Jan 18 '06 #2
CT wrote:
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)
return dynamic_cast<derivedClass_other*>(pData);
...

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

}


V
Jan 18 '06 #3
i think it's unpossible

Jan 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christopher Benson-Manica | last post by:
Are the C functions atoi() and strtol() (preferred, I'm aware) the only way to convert std::string's to integers, or are there other alternatives I'm not aware of? -- Christopher Benson-Manica ...
1
by: Todd Bandrowsky | last post by:
I was just about to make the proclamation that the standard C++'s rtti can't quite do all the stuff that .NET reflection can do, but I thought I would check. Perhaps my C++ knowledge is not quite...
3
by: Firstname Lastname | last post by:
In a function call, for example function(Class(), &Class::memfunc), my compiler cannot match the 2nd argument when: (1) Class is a template parameter, and (2) the base class of Class defines...
3
by: Ganesh | last post by:
Hi, typeinfo class has a private constructor and only typeid can return object to typeinfo class. So here I am wondering how these two are related. Are these two related through 'friend' so that...
7
by: Gary Wessle | last post by:
Hi can I reference the class name from within the object? thanks
6
by: noel.hunt | last post by:
I have a base class, PadRcv, with virtual functions. User code will derive from this class and possibly supply it's own functions to override the base class virtual functions. How can I test that...
5
by: evaristobo | last post by:
i'm a bit confused here, a pointer is something aiming to an object, but a static class is not an object, nevertheless i would like to have the possibility of doing something like handling an...
6
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
13
by: bob the builder | last post by:
Can i get the class of an object of which i only have the address? Lets say i have 3 classes: AClass,BClass,CClass. i also have an object of each class: aObject, bObject, cObject And finally i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.