我不明白这段代码是如何编译的。有人可以解释那里发生了什么。
#include <iostream>
using namespace std;
class B
{
public:
B(const char* str = "\0") //default constructor
{
cout << "Constructor called" << endl;
}
B(const B &b) //copy constructor
{
cout << "Copy constructor called" << endl;
}
};
int main()
{
B ob = "copy me"; //why no compilation error.
return 0;
}
optput 是:构造函数调用
PS:我想不出比这更贴切的标题了,谁能想到更好的标题,请修改。