[英]Function that returns parameters C++
Hello I have a little problem and I would be happy if you could tell me how to fix it. 您好,我有一个小问题,如果您能告诉我如何解决它,我将很高兴。 So I have a function which is the realization like this :
所以我有一个函数是这样的实现:
int* getMytree()
The function should return three variables. 该函数应返回三个变量。 The variables are defined as public.
变量定义为public。
How do I return the three parameters ? 如何返回三个参数?
Apart from wrapping three variables inside a struct
除了将三个变量包装在
struct
You can return a std::tuple
您可以返回一个
std::tuple
std::tuple<int, int, int> getMytree(int id)
{
// ....
// a= , b= c=
return std::make_tuple( a,b,c );
}
And then use std::tie
with your public variables as : 然后将
std::tie
与您的公共变量一起使用:
std::tie( var1, var2, var3 ) = getMytree() ;
Sorry there's no way to explicitly return three values from a function as function can return only one value. 抱歉,由于函数只能返回一个值,因此无法从函数中显式返回三个值。 However you could do two things
但是你可以做两件事
1) Encapsulate those three parameters in a struct and then make function to return that struct. 1)将这三个参数封装在一个结构中,然后使函数返回该结构。
struct Book
{
int pages;
std::string name;
std::string author
};
Book myFunc (); // This example is just for simplicity.
2) More simply pass pointers/references to three parameters you want the function to change. 2)更简单地将指针/引用传递给您想要函数更改的三个参数。
void myFunc ( int& page, std::string &name, std::string &author );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.