I thought operator"=" only call copy construtor or assign construtor, but some different remind me this is wrong. like following code
class B {
public:
explicit B(int* pi)
: i_(pi), deleter(nullptr) {
std::cout << "type convert"<<std::endl;
}
B(int* pi, void (*del)())
: i_(pi), deleter(del) {
std::cout << "double cons"<<std::endl;
}
B(const B& rhs) {
std::cout << "copy"<<std::endl;
}
B& operator=(const B& rhs) {
std::cout << "="<<std::endl;
i_ = rhs.i_;
deleter = rhs.deleter;
return *this;
}
~B() {
std::cout << "dest"<<std::endl;
}
private:
int* i_;
void (*deleter)();
};
void test() {
}
int main(int argc, char* argv[]) {
B pb(new int(4));
B pc = {new int(3), test};
B pd = {new int(3)};
return EXIT_SUCCESS;
}
the output is
type convert
double cons
// pd is wrong
I thought the output is
type convert
double cons ->copy
type convert
of course, pb called type convert constructor. but pc called constructor with two arguments. and pd is wrong called. so my questions:how operator= chose appropriate constructor?