Skip to main content

Posts

Showing posts from November, 2021

C++ property and detection of property change event

In C++, property means a private field with a getter and a setter method. Unlike C# and other languages, it is part of the language where it is set or accessed just like a field. In C++ never had the notion of the way C# address gets or set of property field(s). However, in MSVC, Clang a property extension has been added to give a notion of get / set of property. How it looks like? Let's have a small code:  #include <iostream> #include <string> using std::cout; using std::string; class Animal {     string sName_; public:     string getName() const { return sName_; }     void setName(const string &asName)     {         sName_ = asName;     } public:     __declspec(property(get = getName, put = setName)) string sName; }; int main() {     Animal aObj;     // Set animal name     aObj.sName = "Cat";     cout << "The animal is: " << aObj.sName << "\n"; } The __declspec extension uses the get and set methods, and these two me