class dynamically

Started by
10 comments, last by falconne 15 years, 11 months ago
we can make a class simply by stating class A { int x,y; etc etc } but i want that user at run time should enter the name of the class + some attrributes at run time and then the class should be created at run time that is after that point , we sholud be able to use that class i.e. we should be able to create its objects etc etc how to do that?
Advertisement
It seems a bit backward. Why do you want the User to create classes? You want your User to use the program you make with that classes you have built. Almost sounds like you want the User to progam it for you.

If your refering to something like, for example, you want the user to create an 'enemy'. You would have to design a EmptyEnemyShell class, that the user could then populate with attributes such as 'MovesInAStraightLine' or 'FiresEvery2Seconds'. But these are issues that you will have to make when your building the application.

Essentially you have to give the user some kind of ideas as to what you want them todo, otherwise you will just be creating some kind of higher level language.
the format of all the classes created by the user will be the same.
so can we do it in some way ?
Quote:Original post by iamspecial
the format of all the classes created by the user will be the same.


What do you mean? Can you give an example?
Factory + RAII:

manespace po = boost::program_options;class RuntimeConstrable {  RuntimeConstructable(const po::variable_map & vm)    : x(vm["x"].as<int>())    , y(vm["y"].as<int>())    ...  {}};Factory f;...po::variable_map parameters;...RuntimeConstructible rc = f.newInstance("MyClassName", parameters);


I use boost program_options to achieve that, which allows parameters to a class to be specified in an ini file or via command-line options. Then, you use factory to create an instance on an object.

To avoid too many complications, using std::map to initialize such class is by far the simplest approach.
Quote:Original post by iamspecial
the format of all the classes created by the user will be the same.
so can we do it in some way ?


What do you want the user to be able to do this way? Why does the user want to create classes?
let me reframe my question
(supposing all classes are ofg the same format as
class A
{

int x;
int y;
};
)

suppose there exists a text file of the format
class name value of attributes

and so on

now we have to analyse the file dynamically at the run time and make classes by reading the data from the file. later on , the user may create new objects of the class etc etc

how to do that
thanks
C++ does not have particular good support for this -- whatever hack you employ to attempt this will not result in the same concept from a language perspective as a 'real class.'

You can use a map to store key-value (attribute name - value) pairs that are described in your input text file. This won't be an actual runtime-created 'class' of course, but C++'s type system pretty much makes that impossible.

So, for example, read the file line by line and break up each line into the 'field name' and 'field value' components. Push them all into a map, perhaps std::map<std::string,std::string> and do the conversion later, or use boost::any for the value type, et cetera.

Now you have a user-supplied blob of data you can query. That's all it sounds like you really want to do anyway.
Quote:Original post by iamspecial

how to do that
thanks


See my post above...

It tells you exactly how it's done, using Boost's very robust configuration parsing library.

It's exactly what I'm using it for.
I came up with a similar idea. You'd pretty much have to botch it up yourself, but you can always use pointers to allow users to "create" (read: modify) your class that already exists. You'd end up doing twice the work for this feature, but it gets you what you want.

I had the same problem till i learned about pointers.
Make the class data public, point to the value you want the users to change, have the users input values, and have those values take over what the pointers are pointing to. Viola.

This topic is closed to new replies.

Advertisement