You can emulate getter and setter to some extent by having a member of dedicated type and overriding operator(type) and operator= for it. Whether it's a good idea is another question and I'm going to +1 Kerrek SB's answer to express my opinion thereon :)
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.
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.
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.
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;
}
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!
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;
}
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:
...
}
It provides a example which shows how to create properties, either publicly available as readonly where build-time exception is handled when attempting to write on property; example that shows "default" property with read&write access, example of dynamic getter (alters actual value), and dynamic readonly.