在寻找二叉树实现的示例时,我注意到代码中提供了一些奇怪的东西here。在节点结构的构造函数一个非指针类型的变量被分配给一个指针类型。C++,将非指针类型赋值给模板类的成员指针
它编译得很好(我正在使用GCC 5.3.0)。而让我感到困惑的是编译依赖于其他构造函数的参数,val。
它在类中的方法没有效果,只是在构造函数:
我越来越template <typename T>
class Test {
Test* testPtr;
void testMethod(T t, Test<T> notAPointer) { // OK
this->testPtr = notAPointer;
}
void testMethod(Test<T> notAPointer) { // OK
this->testPtr = notAPointer;
}
Test(T t, Test<T> notAPointer) { // OK
this->testPtr = notAPointer;
}
Test(Test<T> notAPointer) { // compilation error
this->testPtr = notAPointer;
}
};
的编译错误是:
invalid constructor; you probably meant ‘Test (const Test&)’
这是为什么发生?标准中描述的这种行为在哪里?
我认为这是一个错字。它编译得很好,因为所调用的构造函数没有被调用。 – cpplearner
构造'notAPointer'参数时调用哪个构造函数? – emlai
您的构造函数的问题与指针分配或任何伴随问题完全无关。你会得到与空的构造函数体相同的错误。复制构造函数无法通过值接收其参数 - 这就是它的全部内容,而这正是您的编译器试图告诉您的。 – AnT