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
No, C++ has no concept of properties. Though it can be awkward to define and call getThis() or setThat(value), you are making a statement to the consumer of those methods that some functionality may occur. Accessing fields in C++, on the other hand, tells the consumer that no additional or unexpected functionality will occur. Properties would make this less obvious as property access at first glance appears to react like a field, but in fact reacts like a method.
As an aside, I was working in a .NET application (a very well known CMS) attempting to create a customer membership system. Due to the way they had used properties for their user objects, actions were firing off that I hadn't anticipated, causing my implementations to execute in bizarre ways including infinite recursion. This was because their user objects made calls to the data access layer or some global caching system when attempting to access simple things like StreetAddress. Their entire system was founded on what I would call an abuse of properties. Had they have used methods instead of properties, I think I would have figured out what was going wrong much more quickly. Had they have used fields (or at least made their properties behave more like fields), I think the system would have been easier to extend and maintain.
[Edit] Changed my thoughts. I'd had a bad day and went a bit on a rant. This cleanup should be more professional.
You probably know that but I would simply do the following:
class Person {
public:
std::string name() {
return _name;
}
void name(std::string value) {
_name = value;
}
private:
std::string _name;
};
This approach is simple, uses no clever tricks and it gets the job done!
The issue though is that some people don't like to prefix their private fields with an underscore and so they can't really use this approach, but fortunately for these who do, it's really straightforward. :)
The get and set prefixes doesn't add clarity to your API but making them more verbose and the reason I don't think they add useful information is because when someone needs to use an API if the API makes sense she will probably realize what it does without the prefixes.
One more thing, it's easy to grasp that these are properties because name
isn't a verb.
Worst case scenario, if the APIs are consistent and the person didn't realize that name()
is an accessor and name(value)
is a mutator then she will only have to look it up once in the documentation to understand the pattern.
As much as I love C# I don't think C++ needs properties at all!
Nope.. But you should consider if it`s just get : set function and no additional task preformed inside get:set methods just make it public.
With C++11 you can define a Property class template and use it like this:
class Test{
public:
Property<int, Test> Number{this,&Test::setNumber,&Test::getNumber};
private:
int itsNumber;
void setNumber(int theNumber)
{ itsNumber = theNumber; }
int getNumber() const
{ return itsNumber; }
};
And here ist the Property class template.
template<typename T, typename C>
class Property{
public:
using SetterType = void (C::*)(T);
using GetterType = T (C::*)() const;
Property(C* theObject, SetterType theSetter, GetterType theGetter)
:itsObject(theObject),
itsSetter(theSetter),
itsGetter(theGetter)
{ }
operator T() const
{ return (itsObject->*itsGetter)(); }
C& operator = (T theValue) {
(itsObject->*itsSetter)(theValue);
return *itsObject;
}
private:
C* const itsObject;
SetterType const itsSetter;
GetterType const itsGetter;
};
This is not exactly a property, but it does what you want the simple way:
class Foo {
int x;
public:
const int& X;
Foo() : X(x) {
...
}
};
Here the big X behaves like public int X { get; private set; }
in C# syntax. If you want full-blown properties, I made a first shot to implement them here.
There are a set of macros written Here. THis has convinient property declarations for value types, reference types, read only types, strong and weak types.
class MyClass {
// Use assign for value types.
NTPropertyAssign(int, StudentId)
public:
...
}