473,385 Members | 1,356 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,385 software developers and data experts.

Can I use a string to create a object?

Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function

void fun(cha *str)

the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?

Thanks
Dec 14 '07 #1
10 2734
dolphin wrote:
Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function

void fun(cha *str)

the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?
See yesterday's thread 'Is there any """Anti Stringizing operator #"""".

--
Ian Collins.
Dec 14 '07 #2
On 12ÔÂ14ÈÕ, ÏÂÎç4ʱ47·Ö, Ian Collins <ian-n...@hotmail.comwrote:
dolphin wrote:
Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function
void fun(cha *str)
the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?

See yesterday's thread 'Is there any """Anti Stringizing operator #"""".

--
Ian Collins.
Do you mean that I can not do this in C++?
Dec 14 '07 #3
dolphin wrote:
On 12ÔÂ14ÈÕ, ÏÂÎç4ʱ47·Ö, Ian Collins <ian-n...@hotmail.comwrote:
>dolphin wrote:
>>Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function
void fun(cha *str)
the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?
See yesterday's thread 'Is there any """Anti Stringizing operator #"""".
*Please* don't quote signatures.
>
Do you mean that I can not do this in C++?
Not directly, no. Read up on the factory pattern and its use in C++.

--
Ian Collins.
Dec 14 '07 #4
On 12ÔÂ14ÈÕ, ÏÂÎç5ʱ54·Ö, Ian Collins <ian-n...@hotmail.comwrote:
dolphin wrote:
On 12ÔÂ14ÈÕ, ÏÂÎç4ʱ47·Ö, Ian Collins <ian-n...@hotmail.comwrote:
dolphin wrote:
Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function
void fun(cha *str)
the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?
See yesterday's thread 'Is there any """Anti Stringizing operator #""""..

*Please* don't quote signatures.
Do you mean that I can not do this in C++?

Not directly, no. Read up on the factory pattern and its use in C++.

--
Ian Collins.
I know the factory pattern.But the different is that the factory need
know all the type in advance. But in this function,we do not know what
type we will pass.
Dec 14 '07 #5
On 12ÔÂ14ÈÕ, ÏÂÎç5ʱ54·Ö, Ian Collins <ian-n...@hotmail.comwrote:
dolphin wrote:
On 12ÔÂ14ÈÕ, ÏÂÎç4ʱ47·Ö, Ian Collins <ian-n...@hotmail.comwrote:
dolphin wrote:
Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function
void fun(cha *str)
the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?
See yesterday's thread 'Is there any """Anti Stringizing operator #""""..

*Please* don't quote signatures.
Do you mean that I can not do this in C++?

Not directly, no. Read up on the factory pattern and its use in C++.
I know the factory pattern.The different is that the factory need know
all the type that it would create in advance.In this funtion we do not
know what type we will pass.Is there any method?Thanks!
Dec 14 '07 #6
dolphin wrote:
>Hi All I have a question now. Can I use a string to create a object?
For example ,I have a function void fun(cha *str) the str is a name
of a type. If I call the function fun("int"),how do I create a int
type variable.And if there is a class name classtype, I also call the
function fun("classtype"),how do I create a classtype type variable?

I know the factory pattern.But the different is that the factory need
know all the type in advance. But in this function,we do not know what
type we will pass.
It sounds a bit like you have chosen the wrong language. C++ is stronly typed,
so you should always know which classes are available in your code (it's not as
if code could get added at run-time as this is possible with Java). Maybe you
should change to a scripting language (one should always carefully think about
which tools are the most suitable for a given problem space).

I know only a single scenario where the factory pattern actually makes sense:
When you program using some component framework like MS's COM or OMG's CORBA,
you can create components by giving their names to the framework. This way the
user can decide by external configuration files which functionality should be used.

What exactly are you trying to achieve? Maybe your problem can be solved in an
entirely different way?

Regards,
Stuart
Dec 14 '07 #7
dolphin wrote:
>Not directly, no. Read up on the factory pattern and its use in C++.
I know the factory pattern.The different is that the factory need know
all the type that it would create in advance.In this funtion we do not
know what type we will pass.Is there any method?Thanks!
You can create some kind of ad-hoc dynamic type mechanism, usually
consisting of a stub struct (or class) with a type identifier slot which
determines the rest of the "object". It thus mimicks how dynamic
languages do their thing, only written manually in C++ (or C). Sometimes
this is a good way to organize stuff although I have some doubt it is in
your case. What exactly do you need this for?
Dec 14 '07 #8
On Dec 14, 4:37 pm, Stuart Redmann <DerTop...@web.dewrote:
dolphin wrote:
Hi All I have a question now. Can I use a string to create
a object? For example ,I have a function void fun(cha
*str) the str is a name of a type. If I call the function
fun("int"),how do I create a int type variable.And if there
is a class name classtype, I also call the function
fun("classtype"),how do I create a classtype type variable?
I know the factory pattern.But the different is that the
factory need know all the type in advance. But in this
function,we do not know what type we will pass.
It sounds a bit like you have chosen the wrong language. C++
is stronly typed, so you should always know which classes are
available in your code
For the moment, if you limit yourself to the standard. In
practice, under both Posix and Windows, you can load new classes
dynamically; I've done so in specific cases. If you really want
to get tricky, it's also possible to unload them---or to unload
them and then reload them, to update parts of the code without
stopping the application.
(it's not as if code could get added at run-time as this is
possible with Java).
In practice, the constraints aren't much more than they are in
Java. You do have to write a bit more code---on the other hand,
if you don't need this capability (and most programs don't), you
don't pay for it.
Maybe you should change to a scripting language (one should
always carefully think about which tools are the most suitable
for a given problem space).
Scripting languages often go one step further---you can treat
part of the data as code. This can be very useful at times, but
can also lead to very unreadable programs if misused.
I know only a single scenario where the factory pattern
actually makes sense: When you program using some component
framework like MS's COM or OMG's CORBA, you can create
components by giving their names to the framework. This way
the user can decide by external configuration files which
functionality should be used.
It's quite useful for many different types of persistence, as
well. The persistent data contains a type identifier, followed
by the class data.

It's also useful in any number of scenarios where parsing is
involved, or additional dynamicism is required. (I used it for
specifying the encoding of an input file, for example.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 14 '07 #9
On Dec 14, 12:41 am, dolphin <jdxyw2...@gmail.comwrote:
Hi All
I have a question now. Can I use a string to create a object?
For example ,I have a function

void fun(cha *str)

the str is a name of a type. If I call the function fun("int"),how do
I create a int type variable.And if there is a class name classtype, I
also call the function fun("classtype"),how do I create a classtype
type variable?

Thanks
I would make the string the name of a shared library or dll.
And please pass a const char * or even better a const std::string &.
This DLL should export a well-known function by name.
This well known function can be a function to create a factory class
instance
or maybe the finally class directly.
This way you can introduce new functionality which is not known at
compile time.

Dec 15 '07 #10
Peter schrieb:
I would make the string the name of a shared library or dll.
And please pass a const char * or even better a const std::string &.
This DLL should export a well-known function by name.
This well known function can be a function to create a factory class
instance
or maybe the finally class directly.
This way you can introduce new functionality which is not known at
compile time.
I was playing with a similar idea for some generic I/F handler.
There was actually no requirement by customer for this and i didn't find
"spare time" to exercise.
But my approach would have been to do it the other way around:
Let the libraries (which implement the types of objects, that are to be
created by the factory) themselves invoke some (static?) register()
function of the (one!) factory to make them be known by the factory.
I.e. the factory doesn't need to know in advance, which kind of
polymorphic objects it handles. It just needs to know the subset of
their interface needed to construct them. BTW: for the "things" to
contruct, you then "naturally" would already have defined an abstract
base class. And actually this base type is the only type of object the
factory need to return for its make().
Also the "string" that identifies the type of objects could be
registered by the various object types themselves:

static bool(?) factory::register(std::string const& myId)

factory could use a std::map<std::stringto save a list of registered
objects.
And so it could delegate its make() to the make() of the types themselves.

The main module (configuration setup) could just have a configuration
file, that list the libs to load dynamically.

I am pretty sure it will work :-) (at least on unix).

\Frank

Dec 16 '07 #11

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

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
21
by: M D | last post by:
You know how you assume you know until you find out you don't know. Well, I typed into a function definition "..., new String("")). I know what I want. Everyone reading this knows what I want....
7
by: Ioannis Vranos | last post by:
I had reported this as a bug: Description: Default indexed property of System::String crashes for object with stack semantics. int main()
12
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
0
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
2
by: Hetal | last post by:
Hi... I am a newbie VB.NET developer and i am looking at working with ADO.NET rather than ADO. In one of our native VB application with ADO, we used to create 1 connection object and that would...
1
by: Allan Ebdrup | last post by:
I get the error: "Cannot create an object of type 'CustomWizard' from its string representation 'CustomWizard1' for the CustomWizard Property." when I view my custom server web control in...
9
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?

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.