在函数声明中分配参数值? [英] Assigning parameter value in function declaration?

查看:86
本文介绍了在函数声明中分配参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在此构造函数声明中将输入参数分配为2.

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

是什么意思?这是否意味着默认情况下(除非通过了其他操作),大小将为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 :/

提前谢谢!

推荐答案

您是对的,默认情况下该参数值为2.

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

因此您可以正常调用它:

So you can call it normally:

Graph g(5);

在这种情况下,size等于5,

in which case size will be equal to 5,

或者您可以在不提供值的情况下调用它:

or you can call it without providing a value:

Graph g;

在这种情况下,size将等于2.

in which case size will be equal to 2.

注意:Graph g();实际上是一个函数声明,而不是构造/初始化.使用C和C ++,您可以声明其他功能内的功能. Graph g();是函数g的声明,该函数不带参数,并按值返回Graph对象.

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.

这篇关于在函数声明中分配参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆