how overloading the assigment operator with a second argument?

heres my actual overloading assigment operator:
1
2
3
4
5
6
Form & operator= (const std::function<void(HDC WindowHDC)> &Paint)
    {
        this->Paint=Paint;
        Refresh();
        return *this;
    }

how i use it:
1
2
3
4
5
6
7
frMain=[](HDC WindowDC)
    {
        RECT textrec={10,10,100,20};
        Rectangle(WindowDC,10,10,100,30);
        SetBkMode(WindowDC, TRANSPARENT);
        DrawText(WindowDC, "hello World",-1, &textrec, DT_SINGLELINE|DT_NOCLIP);
    };

works fine, but i need another change:
how can i change the overload assigment operator for i do:
frMain.Paint=[](HDC WindowDC)
??
Last edited on
Paint needs to be a public member variable of type std::function<void(HDC WindowHDC)>.
yes it's a public variable include the overloading assignment operator
but why i can't?
1
2
3
4
5
6
Form::std::function<void(HDC WindowHDC)> & operator= (const std::function<void(HDC WindowHDC)> &Paint)
    {
        this->Paint=Paint;
        Refresh();
        return *this;
    }

because the Paint it's: std::function<void(HDC WindowHDC)>
You can't overload the assignment operator for std::function if that's what you're trying to do. If Paint is a public variable you should be able to assign to it directly but that won't call Refresh().
see these code:
1
2
3
4
5
6
std::function<void(HDC WindowHDC)> &operator= (const std::function<void(HDC WindowHDC)> &PPaint)
    {
        Paint=PPaint;
        Refresh();
        return Paint;
    }

1
2
3
4
5
6
7
frMain=[](HDC WindowDC)
    {
        RECT textrec={10,10,100,20};
        Rectangle(WindowDC,10,10,100,30);
        SetBkMode(WindowDC, TRANSPARENT);
        DrawText(WindowDC, "hello World",-1, &textrec, DT_SINGLELINE|DT_NOCLIP);
    };

why works instead: 'frMain.Paint'?
Because you are overloading Form::operator=. Assigning to frMain.Paint will use std::function<void(HDC WindowHDC)>::operator= but you can't overload that.
is like i had created a property default using 'std::function<void(HDC WindowHDC)>'.. strange.
but without a member is useless :(
but the frMain it's a Form and not 'std::function<void(HDC WindowHDC)>'
Are you trying to mimic properties in C# or something? Why don't you just create a member function with a descriptive name and be done with it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Form::setPaintRoutine(const std::function<void(HDC WindowHDC)>& PPaint)
{
    Paint=PPaint;
    Refresh();
}

...

frMain.setPaintRoutine([](HDC WindowDC)
    {
        RECT textrec={10,10,100,20};
        Rectangle(WindowDC,10,10,100,30);
        SetBkMode(WindowDC, TRANSPARENT);
        DrawText(WindowDC, "hello World",-1, &textrec, DT_SINGLELINE|DT_NOCLIP);
    });
Last edited on
i never used C#.
i'm trying simplificate some things.
but you have right, too: we have learned that after change the Paint, we must then Refresh the Window for see it. but i was trying do it in just 1 step. but your idea was the best. thank you so much
Topic archived. No new replies allowed.