问题
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