Assigning parameter value in function declaration?

谁说胖子不能爱 提交于 2020-01-16 03:19:41

问题


I don't understand why in this constructor declaration, the input parameter is assigned 2.

What does it mean? Does it mean that by default (unless something else is passed), size will be 2?

Graph(int size = 2);

I've never seen syntax like this, so I don't even know how to Google it :/

Thanks in advance!


回答1:


You're right, the parameter value will be 2 by default.

So you can call it normally:

Graph g(5);

in which case size will be equal to 5,

or you can call it without providing a value:

Graph g;

in which case size will be equal to 2.

Note: Graph g(); is actually a function declaration, not a construction/initialization. C and C++ allow you to declare functions inside other functions. Graph g(); is the declaration of a function g that takes no arguments and returns a Graph object by value.



来源:https://stackoverflow.com/questions/17961277/assigning-parameter-value-in-function-declaration

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