What is the difference between Object b(); and Object b;?

烈酒焚心 提交于 2019-12-02 04:33:48

The problem is that Student jack(); declares a function with Student as a return type. It doesn't declare an object of that class as you expect.

  Student jack();

declares a function that returns student and takes no arguments. Not an object!

See more in this gotw

"Object b();" declares a function b() returning an object of type Object, while "Object b;" defines a variable b of type Object.

No, it's not obvious, and it still comes back to bite me if I switch between C++, Java, and C#. :-)

What is the difference between Object b(); and Object b;?

The difference exists because C++ interprets that as a function being declared, instead of an object being created.

Object b;

This is the object b of class Object being created by means of the default constructor.

Object b();

This is the function b(), being declared (it will be defined elsewhere) to return an object of class Object, and no parameters.

Hope this helps.

I would try this

class Student {

public:

int gpa = 4;

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