我不明白这段代码是如何编译的。有人可以解释那里发生了什么。C++:为什么代码正在编译
#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是: 构造称为
P.S:我想不出更恰当的标题比这个的,任何人谁能够想到一个更好的标题,请修改它。
你为什么认为它不应该编译? –
指定的行与数据类型不匹配 –