Your code has implicit conversion from A
to B
, the b = a
is compiled as b = B(a);
. If you want this to be detected as error, you can use the explicit
specifier:
struct B {
B(){}
explicit B(const A& a) {
std::cout << "constructor" << std::endl;
}
};
Then you should get error, such as these generated by ideone.com:
prog.cpp: In function 'int main()':
prog.cpp:20:7: error: no match for 'operator=' (operand types are 'B' and 'A')
b = a;
^
prog.cpp:5:8: note: candidate: B& B::operator=(const B&)
struct B {
^
prog.cpp:5:8: note: no known conversion for argument 1 from 'A' to 'const B&'
prog.cpp:5:8: note: candidate: B& B::operator=(B&&)
prog.cpp:5:8: note: no known conversion for argument 1 from 'A' to 'B&&'
After that, the constructor will never be implicitly called, and if you want to call it, you have to write it explicitly: b = B(a);