I'm trying to set up a syntactical sugar similar to the c# property concept.
I've read this post: C#-like properties in native c++?. It's helpful, but lacks the design I want. I also respectfully disagree with several of the assertions made there that the property concept is bad oo form, as I fail to see the difference between a set of methods titled get_id() and set_id() and an operator overload that exposes the same concept, allows code to be cleaner when the class is consumed, and generally discourages the public access to private fields, also decoupling the public implementation from private design.
However, the code that I've come up with (inspired by that link) is REALLY kludgy and will be quite difficult to maintain. I'm wondering if anyone has any suggestions to clean this up, or more importantly, knows a better way to do this.
class someClass
{
public:
struct someProperty_struct{
virtual someProperty_struct& operator=(const int&){return *this;}
virtual operator int(){return 0;}
}someProperty;
};
class derivedClass : someClass
{
int i;
public:
struct intProperty: someClass::someProperty_struct
{
protected:
friend class derivedClass;
derivedClass *outer;
public:
virtual someProperty_struct& operator=(const int& value){
outer->i = value;
return *this;}
virtual operator int(){return outer->i;}
};
derivedClass()
{
intProperty p = intProperty();
p.outer = this;
someProperty = p;
}
};
class consumerClass
{
public:
someClass* data;
consumerClass()
{
data = (someClass*)(new derivedClass());
}
void doSomething()
{
data->someProperty = 7;
int x = data->someProperty;
}
};
EDIT 1: It occurs to me that I didn't reveal anything about my intentions with this. It's going to be used in a scheduling program, and we're going to be doing a lot of comparison and assignment of all the data in the classes. 'someClass' will effectively be an interface on the data (very few methods needed, lots of data that should be relatively transparent). It will be defined in a static library that the executable will link. 'derivedClass' will effectively be an external implementation, implemented in a dll which will be dynamically loaded. The reason for this is to enable "hot swapping" of the dll with another one which implements a different file backend. We have plans to implement xml, sqlite, and mysql storage backends using a plugin system to load them up.
So basically, I need a design that allows someClass to be a virtual interface that is inherited by derivedClass, which is loaded by factory method, passed through the plugin system, and used in the end by consumerClass.