具有默认参数值的函数中的默认对象值

时间:2021-01-15 20:13:44

So far I know that if you want to pass a default value for an argument to a function that is an object, you do it like this:

到目前为止,我知道如果你想将一个参数的默认值传递给一个函数,它是一个对象,你可以这样做:

void function(MyObject obj = MyObject()){
    ...
}

However, I've come around to some interesting syntax lately, which confuses me. What happens when we call the function like this?

然而,我最近发现了一些有趣的语法,这让我很困惑。当我们调用这个函数时会发生什么?

void function(MyObject obj = 0){
    ...
}

Note, we are passing an object, not a pointer. The above code compiles just fine, no errors or warnings. And this always calls the constructor with one argument - MyObject is defined like this:

注意,我们传递的是对象,而不是指针。上面的代码编译得很好,没有错误或警告。它总是用一个参数调用构造函数MyObject是这样定义的:

class MyObject{
public:
    MyObject(double n){std::cout << "Argumented\n";}
    MyObject(){std::cout << "Default\n";}
};

Also, where is this behavior documented (because I searched and couldn't find it)?

而且,这种行为在哪里被记录下来(因为我搜索了但没有找到)?

3 个解决方案

#1


11  

The pararameter defaults to a MyObject implicitly constructed from 0, by calling the MyObject(double) constructor. This constructor allows you to implicitly instantiate MyObjects like this:

通过调用MyObject(double)构造函数,pararameter默认为从0隐式构造的MyObject。这个构造函数允许您隐式实例化myate对象,比如:

MyObject o1 = 0;
MyObject o2 = 420/10.;

If this behaviour is not intended, then make the constructor explicit. This will also require a change to function's default parameter:

如果这个行为不是故意的,那么让构造函数显式地显示出来。这还需要修改函数的默认参数:

explicit MyObject(double n);

and

void function(MyObject obj = MyObject(0));

#2


5  

MyObject as shown has a so-called implicit converting constructor from double. As 0 is okay for double, the effect you see is like you did MyObject(0.0).

如图所示,MyObject有一个所谓的隐式转换构造函数。对于double, 0是可以的,您看到的效果就像您对MyObject(0.0)所做的那样。

#3


4  

Provided that the following expression:

前提是:

MyObject obj = 0;

Compiles and has a value of type MyObject, then a function call taking a MyObject will be able to work in the same way.

编译并具有mytype对象的值,然后接受MyObject的函数调用将能够以相同的方式工作。

Imagine this function signature:

想象一下这个函数签名:

void function(MyObject obj){
    ...
}

And the following caller code:

和以下呼叫者代码:

MyObject obj = 0;
function(obj);

This is equivalent to this second caller code:

这相当于第二个调用者代码:

function(MyObject obj = 0);

Because the value of MyObject obj = 0; is equal to the MyObject which has been constructed in that line of code.

因为MyObject obj的值为0;等于在代码行中构造的MyObject。

You are just going one step forward and providing a default parameter value by taking advantage of the same rule.

您只需向前迈进一步,并利用相同的规则提供默认参数值。

#1


11  

The pararameter defaults to a MyObject implicitly constructed from 0, by calling the MyObject(double) constructor. This constructor allows you to implicitly instantiate MyObjects like this:

通过调用MyObject(double)构造函数,pararameter默认为从0隐式构造的MyObject。这个构造函数允许您隐式实例化myate对象,比如:

MyObject o1 = 0;
MyObject o2 = 420/10.;

If this behaviour is not intended, then make the constructor explicit. This will also require a change to function's default parameter:

如果这个行为不是故意的,那么让构造函数显式地显示出来。这还需要修改函数的默认参数:

explicit MyObject(double n);

and

void function(MyObject obj = MyObject(0));

#2


5  

MyObject as shown has a so-called implicit converting constructor from double. As 0 is okay for double, the effect you see is like you did MyObject(0.0).

如图所示,MyObject有一个所谓的隐式转换构造函数。对于double, 0是可以的,您看到的效果就像您对MyObject(0.0)所做的那样。

#3


4  

Provided that the following expression:

前提是:

MyObject obj = 0;

Compiles and has a value of type MyObject, then a function call taking a MyObject will be able to work in the same way.

编译并具有mytype对象的值,然后接受MyObject的函数调用将能够以相同的方式工作。

Imagine this function signature:

想象一下这个函数签名:

void function(MyObject obj){
    ...
}

And the following caller code:

和以下呼叫者代码:

MyObject obj = 0;
function(obj);

This is equivalent to this second caller code:

这相当于第二个调用者代码:

function(MyObject obj = 0);

Because the value of MyObject obj = 0; is equal to the MyObject which has been constructed in that line of code.

因为MyObject obj的值为0;等于在代码行中构造的MyObject。

You are just going one step forward and providing a default parameter value by taking advantage of the same rule.

您只需向前迈进一步,并利用相同的规则提供默认参数值。