What's the differences between Test t; and Test t();? if Test is a class [duplicate]

微笑、不失礼 提交于 2019-11-28 07:48:53
Luchian Grigore

Test t; defines a variable called t of type Test.

Test t(); declares a function called t that takes no parameters and returns a Test.

What is the Difference between two declarations?

A a(); 

Declares a function and not an object. It is one of the Most vexing parse in C++.
It declares a function by the name a which takes no parameters and returns a type A.

A a;

Creates a object named a of the type A by calling its default constructor.

Why do you get the compilation error?

For a class default access specifier is private so You get the error because your class constructor is private and it cannot be called while creating the object with above syntax.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!