Solved

Dynamic Objects

Posted on 2004-05-02
8
227 Views
Last Modified: 2012-05-04
Hello,

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?

Thanks,
Eugene Byrne
0
Comment
Question by:ebyrne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Accepted Solution

by:
efn earned 34 total points
ID: 10974047
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
{
public:
  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?

--efn
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10974213
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.
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 10975038
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.....

Dennis
0
 

Assisted Solution

by:mahmooh
mahmooh earned 33 total points
ID: 11015545
Heres an answer:

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

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

class CTheObject {
    Vector<CProperty> properties;
Public:
    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);
}
0
 

Assisted Solution

by:YUY0x7
YUY0x7 earned 33 total points
ID: 11024691
Better would be to have the property class templated..

template <class T>
class CProperty
{
private:
    string name;
    T value;

public:
    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,
YUY0x7
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question