C++ 如何在C++中将默认参数设置为类对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12121645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set default parameter as class object in c++?
提问by VarunVyas
I want to set my function with class object parameter set as default. But when I try to do that it fails in compilation.
我想将我的函数设置为默认的类对象参数。但是当我尝试这样做时,它在编译中失败了。
class base {
// ...
};
int myfunc(int a, base b = NULL) {
if (NULL = b) {
// DO SOMETHING
} else {
// DO SOMETHING
}
}
Here when i am trying to compile it, this gives me error that "Default Argument base b have int type"
在这里,当我尝试编译它时,这给了我“默认参数基数 b 具有 int 类型”的错误
回答by Luchian Grigore
Objects can't be NULL
in C++.
对象不能NULL
在 C++ 中。
To set the parameter to default, just use:
要将参数设置为默认值,只需使用:
int myfunc(int a, base b = base())
回答by tenfour
You have three obvious options here.
您在这里有三个明显的选择。
First, use overloads so the caller can choose to pass b
or not.
首先,使用重载以便调用者可以选择是否通过b
。
int myfunc(int a) { ... }
int myfunc(int a, base& b) { ... }
This way you can pass b
without having to use a pointer. Note that you should make b
a reference or pointer type to avoid slicingthe object.
这样你就可以通过b
而不必使用指针。请注意,您应该创建b
引用或指针类型以避免对对象进行切片。
Secondly, if you don't want 2 separate implementations, make b
a pointer, which can be set to NULL
.
其次,如果您不想要 2 个单独的实现,请创建b
一个指针,该指针可以设置为NULL
.
int myfunc(int a, base* b = NULL) { ... }
Third, you could use something to encapsulate the concept of nullable, such as boost::optional
.
第三,你可以用一些东西来封装可空的概念,比如boost::optional
.
int myfunc(int a, boost::optional<base&> b = boost::optional<base&>()) { ... }
回答by user
@tenfouranswer forgot to mention another possible way. You also define a global variable object which you can construct as you like, then set it as the default value:
@tenfour 的回答忘了提及另一种可能的方式。您还可以定义一个可以随意构造的全局变量对象,然后将其设置为默认值:
#include <iostream>
class MyCustomClassType
{
int var;
friend std::ostream &operator<<(
std::ostream &output, const MyCustomClassType &my_custom_class_type )
{
output << my_custom_class_type.var;
return output;
}
};
// C++11 syntax initialization call to the default constructor
MyCustomClassType _my_custom_class_type{};
void function(MyCustomClassType my_custom_class_type = _my_custom_class_type) {
std::cout << my_custom_class_type << std::endl;
}
/**
* To build it use:
* g++ -std=c++11 main.cpp -o main
*/
int main (int argc, char *argv[]) {
function();
}
回答by user
A hack or ugly solution would be do a static cast from null:
黑客或丑陋的解决方案是从 null 进行静态转换:
#include <iostream>
class MyCustomClassType {
int var;
friend std::ostream &operator<<(
std::ostream &output, const MyCustomClassType &my_custom_class_type )
{
output << my_custom_class_type.var;
return output;
}
};
void function(
MyCustomClassType my_custom_class_type = *static_cast<MyCustomClassType*>( nullptr )
)
{
std::cout << my_custom_class_type << std::endl;
}
/**
* To build it use:
* g++ -std=c++11 main.cpp -o main
*/
int main (int argc, char *argv[]) {
function();
}
But, running this gives you directly a segmentation fault due dereferencing a null pointer. I am not sure when this would be useful.
但是,由于取消引用空指针,运行它会直接导致分段错误。我不确定这什么时候会有用。