Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++11 have C#-style properties?

Tags:

c++

c#

class

c++11

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 foo { get; private set; } 

In C++ I have to write

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

Is there some such concept in the C++11 allowing me to have some syntax sugar on this?

like image 463
Radim Vansa Avatar asked Dec 03 '11 14:12

Radim Vansa


People also ask

Does C++11 include C11?

No, C++11 does not support ALL the features of C11. It does not even support all the features of C99. Variable-length arrays, for example, were introduced in C99, but C++ does not yet support them.

What is the meaning of C11?

C11 (formerly C1X) is an informal name for ISO/IEC 9899:2011, a past standard for the C programming language. It replaced C99 (standard ISO/IEC 9899:1999) and has been superseded by C17 (standard ISO/IEC 9899:2018).

What does C11 add?

C11 eliminates these hacks by introducing two new datatypes with platform-independent widths: char16_t and char32_t for UTF-16 and UTF-32, respectively (UTF-8 encoding uses char, as before). C11 also provides u and U prefixes for Unicode strings, and the u8 prefix for UTF-8 encoded literals.


1 Answers

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.

like image 163
psx Avatar answered Sep 23 '22 17:09

psx