所以我有这个代码:
class ConstTest {
public:
explicit ConstTest(char* name) {}
};
int main() {
ConstTest t("blarghgh");
}
它显然可以编译,即使我认为它不应该。由于 C++ 中的字符串文字有 type const char[],而ConstTest构造函数需要一个 const-less char*- not const char*。并且将const指针强制转换为非 const 通常不是 C++ 隐式完成的。
那么,我哪里错了?为什么要编译?我可以合法地修改构造函数中的取消引用指针吗?!