How to Define or Implement C# Property in ISO C++?

后端 未结 3 765
一整个雨季
一整个雨季 2021-01-27 18:09

How to Define or Implement C# Property in ISO C++ ?

Assume following C# code :

int _id;

int ID
{
    get { return _id; }
    set { _id = value; }
}


        
相关标签:
3条回答
  • 2021-01-27 18:28

    Short answer: you can't.

    Long answer: You could try to simulate them via proxy classes, but believe me this is not worth the minor incovenience in having set/get functions.

    You'd have basically to define a class which forwards all the behavior of the variable. This is insanely hard to get right, and impossible to be made generic.

    0 讨论(0)
  • 2021-01-27 18:43

    Quite simply. I'd argue this even has no overhead compared to making the variable public. However, you can't modify this any further. Unless, of course, you add two more template parameters that are call backs to functions to call when getting and setting.

    template<typename TNDataType>
    class CProperty
    {
    public:
        typedef TNDataType TDDataType;
    private:
        TDDataType m_Value;
    public:
        inline TDDataType& operator=(const TDDataType& Value)
        {
            m_Value = Value;
            return *this;
        }
    
        inline operator TDDataType&()
        {
            return m_Value;
        }
    };
    

    EDIT: Don't make the call back functions template parameters, just data members that are constant and must be initialized in the constructor for the property. This inherently has greater overhead than simply writing a get and set method your self, because you're making function calls inside of your gets and sets this way. The callbacks will be set at run-time, not compile-time.

    0 讨论(0)
  • 2021-01-27 18:46

    As Alexandre C. has already stated, it's very awkward and not really worth it, but to give an example of how you might do it.

    template <typename TClass, typename TProperty>
    class Property
    {
        private:
            void (TClass::*m_fp_set)(TProperty value);
            TProperty (TClass::*m_fp_get)();
            TClass * m_class;
    
            inline TProperty Get(void)
            {
                return (m_class->*m_fp_get)();
            }
    
            inline void Set(TProperty value)
            {
                (m_class->*m_fp_set)(value);
            }
    
        public:
            Property()
            {
                m_class = NULL;
                m_fp_set = NULL;
                m_fp_set = NULL;
            }
    
            void Init(TClass* p_class, TProperty (TClass::*p_fp_get)(void), void (TClass::*p_fp_set)(TProperty))
            {
                m_class = p_class;
                m_fp_set = p_fp_set;
                m_fp_get = p_fp_get;
            }
    
            inline operator TProperty(void)
            {
                return this->Get();
            }
    
            inline TProperty operator=(TProperty value)
            {
                this->Set(value);
            }
    };
    

    In your class where you wish to use it, you create a new field for the property, and you must call Init to pass your get/set methods to the property. (pref in .ctor).

    class MyClass {
    private:
        int _id;
    
        int getID() { return _id; }
        void setID(int newID) { _id = newID; }
    public:
        Property<MyClass, int> Id;
    
        MyClass() {
            Id.Init(this, &MyClass::getID, &MyClass::setID);
        }
    };
    
    0 讨论(0)
提交回复
热议问题