Revisiting how to implement __property in ansi c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaleC
    New Member
    • Jul 2014
    • 2

    Revisiting how to implement __property in ansi c++

    I have the same problem as discussed at



    The best solution there looks like this one, but I cannot get it to compile - see embedded comments. Any advice much appreciated.

    Code:
    template <typename T, typename C>
     class Property
     {
     typedef T (C::*Get)() const;
     typedef void (C::*Set)(T);
     Get GetFunc_;
     Set SetFunc_;
     C & Class_;
     public:
     Property(Get GetFunc, Set SetFunc, C &Class)
     : GetFunc_(GetFunc), SetFunc_(SetFunc), Class_(Class) {}
    
     operator T () const
     {
     return (Class_.*GetFunc_)();
     }
    
     Property<T, C>& operator=(T val)
     {
     (Class_.*SetFunc_)(val);
     return *this;
     }
     };
    
     class TMyClass
     {
     int FMyVariable;
     public:
     Property<int, TMyClass> MyVariable;
     // fails at the line below in an MFC compile using VS2013
     // wants argument lists, so I changed to
     // TMyClass() : MyVariable(GetMyVariable(), SetMyVariable(int), *this)
     // but no good.
     TMyClass() : MyVariable(GetMyVariable, SetMyVariable, *this)
     {
     }
     int GetMyVariable() const
     {
     return FMyVariable;
     }
     void SetMyVariable(int Value)
     {
     FMyVariable = Value;
     }
     };
    Last edited by Rabbit; Jul 20 '14, 05:55 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is a problem:

    Code:
    TMyClass() : MyVariable([B]GetMyVariable[/B], [B]SetMyVariable[/B], *this)
     {
     }
     int GetMyVariable() const
     {
     return FMyVariable;
     }
     void SetMyVariable(int Value)
     { 
     FMyVariable = Value;
     }
    Here two function pointers are being used before the compiler has seen the functions.

    Often the trick to templates is to code the classes without using templates until it compiles and links. Then convert the class declarations to templates.

    Comment

    • DaleC
      New Member
      • Jul 2014
      • 2

      #3
      Hi weakness,

      Thanks for the suggestion. Some further googling found a solution. If I change TMyClass constructor to

      <code>
      class TMyClass
      {
      int FMyVariable;
      public:
      Property<int, TMyClass3> MyVariable;
      TMyClass() : FMyVariable(0), MyVariable(&TMy Class::GetMyVar iable, &TMyClass::SetM yVariable, *this) {}
      int GetMyVariable() const
      {
      return FMyVariable;
      }
      void SetMyVariable(i nt Value)
      {
      FMyVariable = Value;
      }
      };
      </code>

      then it compiles and works.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        OK.

        However, in looking at your code it looks like you have a Visitor design pattern in disguise.

        Read this: http://bytes.com/topic/c/insights/67...tterns-visitor

        Let me know what you think.

        Comment

        Working...