I have tried my code to find how copy constructor
works. This is my code:
#include <iostream>
using namespace std;
class B{
public:
B(){
cout << "B()\n";
}
B(const B& b){
cout << "B(const B&)\n";
}
~B(){
cout << "~B()\n";
}
};
int main(){
B b = B();
return 0;
}
For the code B b = B()
, I think the process is like this:
- Call constructor
B()
, print B(), return a temporary object of typeB
; - Call the copy constructor
B(const B&)
, pass the returned object in step 1 as arguments, print B(const B&), initialize variableb
.
But my code just outputs B()
, which means no copy constructor
is called. What is the problem?