473,395 Members | 2,079 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,395 software developers and data experts.

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
Aug 20 '08 #1
10 1895
On Aug 20, 2:41*pm, Sami <s.saminat...@gmail.comwrote:
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.
Aug 20 '08 #2
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.
Aug 20 '08 #3
On Aug 20, 10:41*am, Sami <s.saminat...@gmail.comwrote:
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?
Aug 20 '08 #4
On Aug 20, 5:41*am, Sami <s.saminat...@gmail.comwrote:
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
Aug 20 '08 #5
On Aug 20, 6:22*pm, "AnonMail2...@gmail.com" <AnonMail2...@gmail.com>
wrote:
On Aug 20, 5:41*am, Sami <s.saminat...@gmail.comwrote:
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
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
Aug 21 '08 #6
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

Aug 22 '08 #7
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.
Oct 8 '08 #8
On Aug 20, 11:47*am, Ian Collins <ian-n...@hotmail.comwrote:
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: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
Oct 8 '08 #9
James Kanze wrote:
On Aug 20, 11:47 am, Ian Collins <ian-n...@hotmail.comwrote:
>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.
Oct 9 '08 #10
On Oct 9, 3:59 am, Ian Collins <ian-n...@hotmail.comwrote:
James Kanze wrote:
On Aug 20, 11:47 am, Ian Collins <ian-n...@hotmail.comwrote:
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: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
Oct 9 '08 #11

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

Similar topics

0
by: James-D-Bloom | last post by:
I have developed a fully open source & free application that can alter (or simulate the affect of altering) loaded Java classes at runtime. This means you can change a program at runtime without...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
13
by: interec | last post by:
I have some code in Java that I need to translate into C++. My Java code defines a class hierarchy as follows: // interface IA public interface IA { public abstract void doA(); } //...
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
0
by: Roger Ineichen | last post by:
Hi Tim For a usecase like this, I personaly recommend to defina all interfaces in one module which probably is a namespace if you need alot of interfaces to define. e.g. ...
9
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.