class's: how can i use assigment operator on constructor?

how can i use assignment operator on constructor?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class event
{
private:
    function<void (void)> eventfunction=[](){;};
public:
    event ()
    {
        //nothing;
    }

    event (function<void (void)> callfunction=[](){;})
    {
        eventfunction=callfunction;
    }
    event &operator()()
    {
        eventfunction();
        return *this;
    }

    void operator=(function<void (void)> callfunction)
    {
        eventfunction=callfunction;
    }

    void operator=(event EventInstance)
    {
        eventfunction=EventInstance.eventfunction;
    }

};

event test=[](){cout<<"hello world";};

error message: "conversion from '<lambda()>' to non-scalar type 'event' requested"
why i can't use the assignment instead the constructor? or the lambda functions are different?
Last edited on
1
2
3
4
5
event test; //fix your default constructor, there is ambiguity
test = [](){cout<<"hello world";}; //calling the assignment operator

event test{ [](){cout<<"hello world";} }; //calling the constructor, note the braces
event test={ [](){cout<<"hello world";} }; //also calling the constructor, note the braces 
i forget that important thing about the braces on class instance inicialization and the assignment.
i'm sorry, but why is 'ambiguity'? it's because i use a default parameter on constructor?
Yes, it's because of the default parameter, also known as default arguments according to cppreference
http://en.cppreference.com/w/cpp/language/default_arguments
thank you so much for correct me
Topic archived. No new replies allowed.