In C#, there is a nice syntax sugar for fields with getter and setter. Moreover, I like the auto-implemented properties which allow me to write
public Foo fo
Does you class really need to enforce some invariant or is it just a logical grouping of member elements? If it is the latter you should consider making the thing a struct and accessing the members directly.
In C++ you can write your own features. Here is an example implementation of properties using unnamed classes. Wikipedia article
struct Foo
{
class {
int value;
public:
int & operator = (const int &i) { return value = i; }
operator int () const { return value; }
} alpha;
class {
float value;
public:
float & operator = (const float &f) { return value = f; }
operator float () const { return value; }
} bravo;
};
You can write your own getters & setters in place and if you want holder class member access you can extend this example code.
There is nothing in the C++ language that will work across all platforms and compilers.
But if you're willing to break cross-platform compatibility and commit to a specific compiler you may be able to use such syntax, for example in Microsoft Visual C++ you can do
// declspec_property.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}
int getprop() {
return i;
}
__declspec(property(get = getprop, put = putprop)) int the_prop;
};
int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}
As many other have already said, there's no built-in support in the language. However, if you are targeting the Microsoft C++ compiler you can take advantage of the Microsoft-specific extension for properties which is documented here.
This is the example from the linked page:
// declspec_property.cpp
struct S {
int i;
void putprop(int j) {
i = j;
}
int getprop() {
return i;
}
__declspec(property(get = getprop, put = putprop)) int the_prop;
};
int main() {
S s;
s.the_prop = 5;
return s.the_prop;
}
C++ doesn't have this built in, you can define a template to mimic properties functionality:
template <typename T>
class Property {
public:
virtual ~Property() {} //C++11: use override and =default;
virtual T& operator= (const T& f) { return value = f; }
virtual const T& operator() () const { return value; }
virtual explicit operator const T& () const { return value; }
virtual T* operator->() { return &value; }
protected:
T value;
};
To define a property:
Property<float> x;
To implement a custom getter/setter just inherit:
class : public Property<float> {
virtual float & operator = (const float &f) { /*custom code*/ return value = f; }
virtual operator float const & () const { /*custom code*/ return value; }
} y;
To define a read-only property:
template <typename T>
class ReadOnlyProperty {
public:
virtual ~ReadOnlyProperty() {}
virtual operator T const & () const { return value; }
protected:
T value;
};
And to use it in class Owner
:
class Owner {
public:
class : public ReadOnlyProperty<float> { friend class Owner; } x;
Owner() { x.value = 8; }
};
You could define some of the above in macros to make it more concise.
Maybe have a look at the property class I have assembled during the last hours: https://codereview.stackexchange.com/questions/7786/c11-feedback-on-my-approach-to-c-like-class-properties
It allows you to have properties behaving like this:
CTestClass myClass = CTestClass();
myClass.AspectRatio = 1.4;
myClass.Left = 20;
myClass.Right = 80;
myClass.AspectRatio = myClass.AspectRatio * (myClass.Right - myClass.Left);