Dynamic Objects


I was wondring if someone might be able to tell me if the following is possible, and how I would go about doing it. I would like to create the equivalent of an object instantiation on the fly. For example, say I have the class Property, I would like to create a property at runtime. Say the user states that color is a property, then I would like an object color of type Property that can be used. Is there a way this, or something to a similar effect, can be done during runtime?

Eugene Byrne
Who is Participating?
efnConnect With a Mentor Commented:
You can't refer to a variable named "color" at run time without declaring it at compile time.  But you can store a name in an object and use it to find the object at run time.

For example you could do stuff like this:

class Property
  string name;
  // ...

Property x;

//  Get some string like "color" as a name from the user in variable userpropertyname.

x.name = userpropertyname;

//  Now there is a Property that stores the string "color" as its name.  The program still knows it as x, though.

Does this answer your question?

Are you using c++ builder by chance where the usage of properties can be defined through the compiler directive __property?  If that is the case the can not be created dynamically for an object.
can you be more precise in your question ?

Are you asking How do we create an object of a class in runtime with some special property. If yes then create a constructor for that class which takes your property as a parameter and create that object on runtime by passing that paramter.

Or you asking something related to GUI ? do you want to add some color or button on runtime to your GUI or some thing like that ?????

Can you be more clear so that we might help you to clear your doubt.....

mahmoohConnect With a Mentor Commented:
Heres an answer:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class CProperty {
    string name;
    int value;
    CProperty(string n, int v) : name(n), value(v) {}

class CTheObject {
    Vector<CProperty> properties;
    void AddProperty(string name, int value) {
        properties.push_back(CProperty(name, vlaue) );
    CTheObject() {
        AddProperty("text", 7);  // or something
        AddProperty("size", 88);

main() {
    CTheObject o;
    o.AddProperty("color", 23);
YUY0x7Connect With a Mentor Commented:
Better would be to have the property class templated..

template <class T>
class CProperty
    string name;
    T value;

    CProperty( const string& Name, const T& Value ) : name(Name), value(Value) {}

    string getName() const { return name; }
    T getValue() const { return value; }

Of course, it can be done better than that (much better).

Hope it helps,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.