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函数声明*谢谢!这是有道理的。