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

前端 未结 15 1002
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 03:16

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         


        
相关标签:
15条回答
  • 2020-11-28 03:34

    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.

    0 讨论(0)
  • 2020-11-28 03:34

    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!

    0 讨论(0)
  • 2020-11-28 03:36

    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.

    0 讨论(0)
  • 2020-11-28 03:38

    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;
    };
    
    0 讨论(0)
  • 2020-11-28 03:40

    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.

    0 讨论(0)
  • 2020-11-28 03:41

    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:
     ...
    
    }
    
    0 讨论(0)
提交回复
热议问题