Google Groups Home
Help | Sign in
Construction of classes at runtime
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Sami  
View profile
 More options Aug 20, 10:41 am
Newsgroups: comp.lang.c++
From: Sami <s.saminat...@gmail.com>
Date: Wed, 20 Aug 2008 02:41:00 -0700 (PDT)
Subject: Construction of classes at runtime
Hi,

Is there anyway to construct a object of class with its class name
obtained dynamically? Prototype and Factory method along with Abstract
factory design pattern can be used to do it. But it involves using of
if-else statement at the factory level. To avoid using the if-else
statements (there would be plenty in my case), is there any way to
construct the object based on the value of a string variable ? So that
when the class name is assigned to the string variable, the string
value (class name) can be used in construction of the object.

Your response is appreciated.

Thanks,
Sami


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sami  
View profile
 More options Aug 20, 10:44 am
Newsgroups: comp.lang.c++
From: Sami <s.saminat...@gmail.com>
Date: Wed, 20 Aug 2008 02:44:50 -0700 (PDT)
Local: Wed, Aug 20 2008 10:44 am
Subject: Re: Construction of classes at runtime
On Aug 20, 2:41 pm, Sami <s.saminat...@gmail.com> wrote:

> Hi,

> Is there anyway to construct a object of class with its class name
> obtained dynamically? Prototype and Factory method along with Abstract
> factory design pattern can be used to do it. But it involves using of
> if-else statement at the factory level. To avoid using the if-else
> statements (there would be plenty in my case), is there any way to
> construct the object based on the value of a string variable ? So that
> when the class name is assigned to the string variable, the string
> value (class name) can be used in construction of the object.

> Your response is appreciated.

> Thanks,
> Sami

The implementation needed is in C++, hence posted in this group.
Thanks.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile
 More options Aug 20, 10:47 am
Newsgroups: comp.lang.c++
From: Ian Collins <ian-n...@hotmail.com>
Date: Wed, 20 Aug 2008 21:47:59 +1200
Local: Wed, Aug 20 2008 10:47 am
Subject: Re: Construction of classes at runtime

Sami wrote:
> Hi,

> Is there anyway to construct a object of class with its class name
> obtained dynamically? Prototype and Factory method along with Abstract
> factory design pattern can be used to do it. But it involves using of
> if-else statement at the factory level. To avoid using the if-else
> statements (there would be plenty in my case), is there any way to
> construct the object based on the value of a string variable ?

Use a map of strings (names) and factory objects to create the required
class.

You can't create a class 'on the fly' from its name in C++, you have to
know the classes at compile time.

--
Ian Collins.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Harsh Puri  
View profile
 More options Aug 20, 1:36 pm
Newsgroups: comp.lang.c++
From: Harsh Puri <harsh.p...@gmail.com>
Date: Wed, 20 Aug 2008 05:36:53 -0700 (PDT)
Local: Wed, Aug 20 2008 1:36 pm
Subject: Re: Construction of classes at runtime
On Aug 20, 10:41 am, Sami <s.saminat...@gmail.com> wrote:

> Hi,

> Is there anyway to construct a object of class with its class name
> obtained dynamically? Prototype and Factory method along with Abstract
> factory design pattern can be used to do it. But it involves using of
> if-else statement at the factory level. To avoid using the if-else
> statements (there would be plenty in my case), is there any way to
> construct the object based on the value of a string variable ? So that
> when the class name is assigned to the string variable, the string
> value (class name) can be used in construction of the object.

> Your response is appreciated.

> Thanks,
> Sami

A virtual copy constructor, or a clone function might help you?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AnonMail2005@gmail.com  
View profile
 More options Aug 20, 2:22 pm
Newsgroups: comp.lang.c++
From: "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
Date: Wed, 20 Aug 2008 06:22:03 -0700 (PDT)
Local: Wed, Aug 20 2008 2:22 pm
Subject: Re: Construction of classes at runtime
On Aug 20, 5:41 am, Sami <s.saminat...@gmail.com> wrote:

> Hi,

> Is there anyway to construct a object of class with its class name
> obtained dynamically? Prototype and Factory method along with Abstract
> factory design pattern can be used to do it. But it involves using of
> if-else statement at the factory level. To avoid using the if-else
> statements (there would be plenty in my case), is there any way to
> construct the object based on the value of a string variable ? So that
> when the class name is assigned to the string variable, the string
> value (class name) can be used in construction of the object.

> Your response is appreciated.

> Thanks,
> Sami

Take a look at Modern C++ Design.  It's factory implementation uses no
if-else statements.  Essentially, it uses a map of std::strings to
function pointers.

HTH


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sami  
View profile
 More options Aug 21, 7:01 am
Newsgroups: comp.lang.c++
From: Sami <s.saminat...@gmail.com>
Date: Wed, 20 Aug 2008 23:01:21 -0700 (PDT)
Local: Thurs, Aug 21 2008 7:01 am
Subject: Re: Construction of classes at runtime
On Aug 20, 6:22 pm, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:

Thanks Ian, Harsh Puri for the responses.

Thanks HTH, it is a nice idea to have a map.

Also is there any way to implement reflection concept of java in C++.
I know there is no support in native C++ language. Is there any
pattern similar to that in C++?

Thank you
Sami


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r.gr...@science-computing.de  
View profile
 More options Aug 22, 7:31 am
Newsgroups: comp.lang.c++
From: r.gr...@science-computing.de
Date: Thu, 21 Aug 2008 23:31:19 -0700 (PDT)
Local: Fri, Aug 22 2008 7:31 am
Subject: Re: Construction of classes at runtime
Hallo,

> Also is there any way to implement reflection concept of java in C++.
> I know there is no support in native C++ language. Is there any
> pattern similar to that in C++?

have a look at Pattern-Oriented Software Architecture: A System of
Patterns.
There must be a description of implementing reflection. The mapping
from
strings to Functions Pointers is known as dispatch table.

Greetings Rainer


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sami  
View profile
 More options Oct 8, 11:39 am
Newsgroups: comp.lang.c++
From: Sami <s.saminat...@gmail.com>
Date: Wed, 8 Oct 2008 03:39:31 -0700 (PDT)
Local: Wed, Oct 8 2008 11:39 am
Subject: Re: Construction of classes at runtime
On Aug 22, 11:31 am, r.gr...@science-computing.de wrote:

> Hallo,

> > Also is there any way to implement reflection concept of java in C++.
> > I know there is no support in native C++ language. Is there any
> > pattern similar to that in C++?

> have a look at Pattern-Oriented Software Architecture: A System of
> Patterns.
> There must be a description of implementing reflection. The mapping
> from
> strings to Functions Pointers is known as dispatch table.

> Greetings Rainer

Thanks Rainer for the info. It helped.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Oct 8, 9:24 pm
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Wed, 8 Oct 2008 13:24:44 -0700 (PDT)
Local: Wed, Oct 8 2008 9:24 pm
Subject: Re: Construction of classes at runtime
On Aug 20, 11:47 am, Ian Collins <ian-n...@hotmail.com> wrote:

> Sami wrote:
> You can't create a class 'on the fly' from its name in C++,
> you have to know the classes at compile time.

That's true for pretty much every language, isn't it.  It's not
sufficient to just have a name; you need more information from
somewhere.  And you can make things pretty dynamic in C++; I
have one case where I look for a dynamically linked object for
the class if it isn't already loaded.

--
James Kanze (GABI Software)             email:james.ka...@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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile
 More options Oct 9, 2:59 am
Newsgroups: comp.lang.c++
From: Ian Collins <ian-n...@hotmail.com>
Date: Thu, 09 Oct 2008 14:59:56 +1300
Local: Thurs, Oct 9 2008 2:59 am
Subject: Re: Construction of classes at runtime
James Kanze wrote:
> On Aug 20, 11:47 am, Ian Collins <ian-n...@hotmail.com> wrote:
>> Sami wrote:
>> You can't create a class 'on the fly' from its name in C++,
>> you have to know the classes at compile time.

> That's true for pretty much every language, isn't it.  It's not
> sufficient to just have a name; you need more information from
> somewhere.  And you can make things pretty dynamic in C++; I
> have one case where I look for a dynamically linked object for
> the class if it isn't already loaded.

True, but a compiled language will never match the runtime flexibility
of an interpreted one.  For example in PHP to process an RPC request I
can write

call_user_func_array( array($object,$method), $parameters  );

Where $object is the name of a class, $method is the method to call and
$parameters an array of parameters.  The interpreter will find the
class, construct it and call the method.  I could write the same
function in C++, but the tables of classes and methods would have to
explicitly constructed.

--
Ian Collins.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Oct 9, 8:55 am
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Thu, 9 Oct 2008 00:55:40 -0700 (PDT)
Local: Thurs, Oct 9 2008 8:55 am
Subject: Re: Construction of classes at runtime
On Oct 9, 3:59 am, Ian Collins <ian-n...@hotmail.com> wrote:

> James Kanze wrote:
> > On Aug 20, 11:47 am, Ian Collins <ian-n...@hotmail.com> wrote:
> >> Sami wrote:
> >> You can't create a class 'on the fly' from its name in C++,
> >> you have to know the classes at compile time.
> > That's true for pretty much every language, isn't it.  It's not
> > sufficient to just have a name; you need more information from
> > somewhere.  And you can make things pretty dynamic in C++; I
> > have one case where I look for a dynamically linked object for
> > the class if it isn't already loaded.
> True, but a compiled language will never match the runtime
> flexibility of an interpreted one.

Yes.  There are, in fact, several different levels of
flexibility possible (with, generally, more flexibility implying
less robustness).  In totally dynamic languages, with fully
dynamic typing, a "struct" or a "class" is really nothing more
than an associative array; an array whose elements are indexed
by the name of the field.  In such cases, you can dynamically
read a set of attribute value pairs, and use the results as a
struct.  In most compiled languages, and some interpreted ones
as well, I think, the program can only deal with structs known
to it.  With dynamic linking, however, it is possible to make
the set of such structs open, to add to it at runtime.

> For example in PHP to process an RPC request I can write
> call_user_func_array( array($object,$method), $parameters  );
> Where $object is the name of a class, $method is the method to
> call and $parameters an array of parameters.  The interpreter
> will find the class, construct it and call the method.  I
> could write the same function in C++, but the tables of
> classes and methods would have to explicitly constructed.

Or automatically generated by some other program:-).  I'm not
familiar with PHP, but what you are describing doesn't sound too
different from Java's java.lang.Class.forName( "className" )
.newInstance().  As I said, I've actually implemented this once
in C++, with the programming looking for a corresponding
.so/.dll in a path if the desired class wasn't already present.
(It's interesting to note that when I did more or less the same
thing in Java, it was almost as many lines of code.  Since, of
course, not just any class would do; the class had to meet
certain requirements, and I needed code to test those.  In C++,
the .so/.dll wouldn't link if the class didn't meet my
requirements.)

Implementing total flexibility is also more or less possible;
your actual instance is basically an std::map< std::string,
boost::any >.  Of course, total flexibility often results in
total chaos, but like everything else, with a good enough
process and some programmer discipline, it can be made to work.
If you really, really need it.

--
James Kanze (GABI Software)             email:james.ka...@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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google