1

I'm new to the site (and to c++) so please forgive me if this is a basic question - I've googled and looked through this site without success so far, so any help anyone can provide would be hugely appreciated.

I'd like to add some functionality to an app, that allows a user to fully define the structure and contents of an object. For example, user would be presented with a configuration screen that allows them to list each property of the object - given my limited knowledge I've assumed this might be achieved by using a class:

Class Name:        CustomClassName
Class Property 1:  property1Name    property1DataType    property1DefaultValue
...
Class Property n:  propertynName    propertynDataType    propertynDefaultValue

The user would then be able to hit a button to save their custom configuration, and the program could then reference that configuration as a Class:

class CustomClassName
{
    property1DataType property1Name = property1DefaultValue;
    ...
    propertynDataType propertynName = propertynDefaultValue;
}

I'm not even sure this is possible using Classes, so if there's another mechanism that facilitates this I'm open to suggestions!

6
  • Have you heard about JSON or XML ? Commented Mar 11, 2015 at 12:42
  • 1
    Dinamically typed languages can be helpful for this kind of situation. C++ is statically typed so you define a class and won't be able to change it at runtime. A different approach would be to have a class with a container of "properties" and having each property represent a different type (BaseProperty, StringProperty:public BaseProperty)... Perhaps if you tell us more about the objective of your app we could help. Commented Mar 11, 2015 at 12:46
  • Hi David - I'm aware of XML but don't have an understanding of how it could be used within a C++ app.. I'll have a read around to see if anything makes sense.. thanks Commented Mar 11, 2015 at 13:05
  • Hi @TheMarlboroMan - the end goal is to allow users to hold details of multiple ongoing projects, and organise them visually (along the lines of a 'kanban board') so ideally they would have the flexibility to create their own 'types' of project, with each project type needing to hold slightly different data... I hope that makes sense? Commented Mar 11, 2015 at 13:11
  • 1
    You might find Boost.PropertyTree useful Commented Mar 11, 2015 at 13:11

1 Answer 1

1

You can't create classes in runtime, but since dynamic typing is in essence a subset of static typing, you can fake it.

Start with the Property type1:

using Property = variant<int, float, string>;

A simple "dynamic" class could look like this:

class DynamicClass {
    std::map<std::string, Property> properties;
public:
    Property const& operator[](std::string const&) const
    Property operator[](std::string const&);
};

Use:

DynamicClass d;
d["myInt"] = 5;

1 Example implementation. Internals of variant should be tailored for your specific purpose. If you need an open variant, where you don't know all of the possible types beforehand, this gets more complicated, calling for something like any.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.