Sign in to follow this  
iamspecial

class dynamically

Recommended Posts

iamspecial    100
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?

Share this post


Link to post
Share on other sites
JimmyDeemo    156
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.

Share this post


Link to post
Share on other sites
Gage64    1235
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?

Share this post


Link to post
Share on other sites
Antheus    2409
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.

Share this post


Link to post
Share on other sites
Zahlman    1682
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?

Share this post


Link to post
Share on other sites
iamspecial    100
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

Share this post


Link to post
Share on other sites
jpetrie    13138
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.

Share this post


Link to post
Share on other sites
korsen    122
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.

Share this post


Link to post
Share on other sites
superpig    1825
Quote:
Original post by iamspecial
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


Are you saying that all you want to change for each class is the name and the values stored in x and y? Like, say x and y were actually called 'health' and 'power' - and what you're seeking to do is to define a class for each unit in your game, each one with a different health and power value, and have those classes read in from a file? Each class would still have two integer variables called 'health' and 'power', they'd just have different values. Is that what you're trying to do?

Like, would your config file just look something like:

A 1 2
B 3 7
C 2 52

and so on?

Share this post


Link to post
Share on other sites
falconne    136
Post more details of what exactly you're trying to achieve. I doubt "dynamic class definition" is what you're looking for. Sounds like you just need to design a class that's more flexible at runtime than what you currently have.

Someone will be able to explain how to achieve what you need easily.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this