Let this code:
Test1 t1; Test2 t2; t1 = t2;
I believe that there are three (or more?) Ways to implement t1 = t2
- reload
Test1 statement in Test1 - for operator like overload in
Test2 - to create the transform constructor
Test1(const Test2&)
According to my GCC testing, this is the priority of use:
- assign an operator
- conversion constructor and type casting operator (ambiguous)
- const conversion constructor and const type merge operator (ambiguous)
Please help me understand why this priority.
I use this code for testing (uncomment some lines to try)
struct Test2; struct Test1 { Test1() { } Test1(const Test2& t) { puts("const constructor wins"); } // Test1(Test2& t) { puts("constructor wins"); } // Test1& operator=(Test2& t) { puts("assign wins"); } }; struct Test2 { Test2() { } // operator Test1() const { puts("const cast wins"); return Test1(); } // operator Test1() { puts("cast wins"); return Test1(); } }; int main() { Test1 t1; Test2 t2; t1 = t2; return 0; }
source share