C++ 隐藏默认构造函数
下面的代码为什么要编译C++ 隐藏默认构造函数,c++,C++,下面的代码为什么要编译 class Test { public: Test(int i) {} private: Test(); }; int main() { // OK - uses Test(int i) Test test(5); // Error - Test() is private // Test test2; // Why does this compile? Test() is priv
class Test
{
public:
Test(int i) {}
private:
Test();
};
int main()
{
// OK - uses Test(int i)
Test test(5);
// Error - Test() is private
// Test test2;
// Why does this compile? Test() is private!
Test test3();
}
我认为最后一个实例将无法编译,因为无参数构造函数是私有的?
testtest3()代码>是一个函数声明。它声明了类型为int()
的名为test3
的函数。在C++中声明事物是可以的,即使它们从不被定义(因为你并没有试图真正调用它)。< /P> <代码> TestTest3();代码>被视为函数声明,而不是对象的构造。@RakibulHasan函数声明*谢谢!这是有道理的。