see my property constructor:

template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(const T &value)
    {
        getf=nullptr;
        setf=nullptr;
        PropertyValue=value;
    }

    property(const property &value)  :  PropertyValue(value.PropertyValue) , getf(value.getf)
    {
    }

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
    {
        setf=SetFunction;
        getf=GetFunction;
    }

inside a class i have 2 functions:

void SetCaption(string strtext)
    {
        strCaption=strtext;
        RECT textrect={0};
        HDC txthdc=GetDC(hwnd);
        DrawText (txthdc,strCaption.c_str(),strCaption.size(),&textrect,DT_CALCRECT | DT_LEFT);
        SetWindowPos(hwnd, HWND_TOP, 0, 0, textrect.right-textrect.left, textrect.bottom-textrect.top, SWP_NOMOVE);
    }

    string GetCaption()
    {
        return strCaption;
    }

heres how i create the property:
(to complex yes :( )

property <string> caption{std::bind(&tooltip::GetCaption, *this),std::bind(&tooltip::SetCaption, *this, std::placeholders::_1)};

no error message. but my application gives me an error(no message.. just tells me that the programs stops) and the OS closes the program.
what i'm doing wrong?

Recommended Answers

All 2 Replies

Nothing complex just a typo/minor miss-understanding.
You wrote this:

property <string> caption{std::bind(&tooltip::GetCaption, *this),std::bind(&tooltip::SetCaption, *this, std::placeholders::_1)};

Note the *this , that should be just : this. You are trying to binding a class method pointer to a class instance pointer. If the class instance pointer is no such thing then things go wrong. You can use a reference but you convert it with std::ref( X ) which is a bit odd looking if you already have the pointer this!

If we take your orginal protery class and add two methods:

std::string Get() const { return getf(); }
void Set(const std::string& Input) { setf(Input); }

We can write:

class testCase
{
  std::string Hold;

 public:

  testCase() : Hold("WRONG") {}

  void setCaption(const std::string& Out)
    { std::cout<<"SetCaption "<<Out<<std::endl;
      Hold=Out; }

  std::string getCaption() const { return Hold; }

  void build() {
    property <std::string> captionInner
      (std::bind(&testCase::getCaption,this),
       std::bind(&testCase::setCaption,this,std::placeholders::_1));

    captionInner.Set("gamma");
    std::cout<<captionInner.Out()<<std::endl;
    return; }
};

int main()
{
   testCase A;
   A.build();
}

Not that you can try with *this and see the wrong result -- however I believe this is undefined behaviour -- no 100% sure.

for correct it i nust take off the '*' pointer operator and i create precompiled routines:

#define GetProperty(x) std::bind(&x, this)
#define SetProperty(x) std::bind(&x, this, std::placeholders::_1)

//how use it:
property <string> caption{GetProperty(tooltip::GetCaption),SetProperty(tooltip::SetCaption)};

thanks for all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.