Does C ++ 11 have C # style properties?

C # has good syntactic sugar for getter and setter fields. Moreover, I like automatically implemented properties that allow me to write

public Foo foo { get; private set; } 

In C ++, I have to write

 private: Foo foo; public: Foo getFoo() { return foo; } 

Is there any such concept in C ++ 11 that allows me syntactic sugar on it?

+75
c ++ c # class c ++ 11
Dec 03 '11 at 2:59 p.m.
source share
15 answers

No, it is not.

However, the philosophy of C ++ is to promote encapsulation. Whenever you find that your interface simply passes through the member elements ("getters and setters"), think again. Your class should have a full interface for what the class does, and not just serve as a free container for things. Member objects should be implementation details, and not be part of the public interface. If you need a free container, just use std::tuple or something like that.

+12
Dec 03 2018-11-12T00:
source share

In C ++, you can write your own functions. Here is an example implementation of properties using nameless classes. Wiki 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 methods for getting and setting, and if you want to access the owner class, you can extend this code example.

+66
Apr 16 '14 at 12:30
source share

C ++ does not have this built in, you can define a template to simulate the functionality of properties:

 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 get / install method, simply 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 use it in the Owner class :

 class Owner { public: class : public ReadOnlyProperty<float> { friend class Owner; } x; Owner() { x.value = 8; } }; 

You can define some of the above in macros to make it more concise.

+36
Feb 18 '16 at 10:18
source share

You can to some extent emulate a getter and setter by using an element of a selected type and overriding operator(type) and operator= . A good idea is another question, and I'm going to answer +1 Kerrek SB to express my opinion on this :)

+17
Dec 03 2018-11-12T00:
source share

Perhaps look at the property class that I have compiled over the past hours: https://codereview.stackexchange.com/questions/7786/c11-feedback-on-my-approach-to-c-like-class-properties

It allows you to behave like this:

 CTestClass myClass = CTestClass(); myClass.AspectRatio = 1.4; myClass.Left = 20; myClass.Right = 80; myClass.AspectRatio = myClass.AspectRatio * (myClass.Right - myClass.Left); 
+16
Jan 13 2018-12-01T00:
source share

As many others have said, there is 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 the properties described here.

This is an 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; } 
+13
Mar 09 '16 at 16:25
source share

With C ++ 11, you can define a template for the Property class 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 is the template for the Property 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; }; 
+13
Nov 06 '16 at 9:53 on
source share

In C ++, there is nothing that could work on all platforms and compilers.

But if you want to break cross-platform compatibility and commit a specific compiler, you can 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; } 
+12
Mar 12 '18 at 7:33
source share

No, C ++ has no concept of properties. Although it may be inconvenient to define and call getThis () or setThat (value), you make an expression to the consumer about those methods that some functions may have. On the other hand, access to fields in C ++ tells the consumer that there will be no additional or unexpected functions. Properties would make this less obvious, since accessing properties at first glance seems to react like a field, but actually reacts like a method.

As an aside, I was working in a .NET application (a very famous CMS) trying to create a client membership system. Due to the way they used properties for their custom objects, the actions were fired due to what I did not expect, which led to the fact that my implementations were performed in bizarre ways, including infinite recursion. This is because their user objects caused access to the data access layer or to some global caching system when trying to access simple things like StreetAddress. Their whole system was based on what I would call abuse of properties. If they used methods instead of properties, I would have thought that was much faster. If they used fields (or at least made their properties more like fields), I think the system would be easier to expand and maintain.

[Edit] Changed my mind. I had a bad day and I got a little angry. This cleaning should be more professional.

+11
Dec 26 '13 at 17:44
source share

Based on https://stackoverflow.com/a/2129602/2126 , a version with a public recipient and a private installer is presented here:

 struct Foo { class { int value; int& operator= (const int& i) { return value = i; } friend struct Foo; public: operator int() const { return value; } } alpha; }; 
+10
Oct 08 '14 at 19:40
source share

You probably know this, but I would just 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, does not use smart tricks, and it does its job!

The problem is that some people do not like the prefix of their private fields with underscores, and therefore they cannot use this approach, but, fortunately for those who do this, it is very simple. :)

The get and set prefixes do not add clarity to your API, but make them more detailed, and the reason why I do not think they add useful information is that when someone should use the API, if the API makes sense, she will probably understand what he is doing without prefixes.

One more thing, it's easy to understand that these are properties, because name not a verb.

In the worst case, if the APIs are consistent and the person does not understand that name() is an accessory and name(value) is a mutator, then she will have to look for it only once in the documentation to understand the pattern.

As much as I love C #, I don't think C ++ needs properties at all!

+5
Mar 18 '16 at 22:46
source share

This is not quite a property, but it does what you want in a simple way:

 class Foo { int x; public: const int& X; Foo() : X(x) { ... } }; 

Here, big X behaves like public int X { get; private set; } public int X { get; private set; } public int X { get; private set; } in C # syntax. If you want full-blown properties, I took the first shot to implement them here .

+4
Nov 07 '14 at 18:52
source share

No ... But you should think, if it's just a get: set function and no additional tasks are performed inside get: set, the methods just make it public.

+3
Mar 12 '18 at 7:05
source share

Do you need a class to force some invariant or is it just a logical grouping of member elements? If this is the last, you should consider creating a structure and accessing members directly.

+1
Dec 04 '11 at 4:16
source share

I compiled ideas from several C ++ sources and placed them in a nice, still pretty simple example for the get / install methods in C ++:

 class Canvas { public: void resize() { cout << "resize to " << width << " " << height << endl; } Canvas(int w, int h) : width(*this), height(*this) { cout << "new canvas " << w << " " << h << endl; width.value = w; height.value = h; } class Width { public: Canvas& canvas; int value; Width(Canvas& canvas): canvas(canvas) {} int & operator = (const int &i) { value = i; canvas.resize(); return value; } operator int () const { return value; } } width; class Height { public: Canvas& canvas; int value; Height(Canvas& canvas): canvas(canvas) {} int & operator = (const int &i) { value = i; canvas.resize(); return value; } operator int () const { return value; } } height; }; int main() { Canvas canvas(256, 256); canvas.width = 128; canvas.height = 64; } 

Exit:

 new canvas 256 256 resize to 128 256 resize to 128 64 

You can check it online here: http://codepad.org/zosxqjTX

0
Oct 25 '18 at 10:56
source share



All Articles