有人可以解释为什么我们会收到“错误:无法直接调用构造函数“ Test :: Test”的错误”吗?

时间:2019-02-11 18:32:20

标签: c++

class Test
{
public:
    Test(){
        cout<<"Inside ctor"<<endl;
    }

    ~Test()
    {
        cout<<"Inside dtor"<<endl;
    }

};

int main() {

    Test t;         //works fine 
    t.~Test();      //works fine
    Test();         //works fine
    t.Test::Test();  //gives error... Why??

    return 0;
}

**`Error`**
Building file: ../src/testCtor.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testCtor.d" -MT"src/testCtor.o" -o "src/testCtor.o" "../src/testCtor.cpp"
../src/testCtor.cpp: In function ‘int main()’:
../src/testCtor.cpp:37:10: error: cannot call constructor ‘Test::Test’ directly
  t.Test::Test();  //error
          ^~~~

我尝试搜索解决方案,但是找不到任何具体的东西。有人可以帮我解释为什么这样做是错误的吗?

预先感谢.. !!

0 个答案:

没有答案