如何使用 *pointer 从函数中获取第二个值?

How I can get second value from function using *pointer?

我有一个函数,我需要从那里得到两个值。函数可以return只有一个值。我在某处读到我可以使用指针获取第二个值,当我尝试这样做时出现错误,因此看起来指针值不能超出可见区域。所以这意味着我只有一个值。如何使用指针从函数中获取第二个值?

某种类型的实例是 "one value"。考虑这个 class:

struct foo {
     int bar;
     double moo;
};

现在我可以写一个函数 returns 一个 那个类型的值:

foo asdf() { 
    foo result;
    result.bar = 42;
    result.moo = 3.14159;
    return result;
}

PS:不要使用指针。可以使用所谓的输出参数,但如果你这样做,你应该使用引用而不是指针:

void asdf2(int& bar,int& moo) {
     bar = 42;
     moo = 3.14159;
}

这样调用:

int x = 0;
double y = 0.;
asdf2(x,y);

然而,out 参数并不好,因为在 foo f = asdf(); 中,与 asdf2(x,y) 相比,函数返回的内容更明显。

你在某处读到的想法是调用函数的代码,创建对象,将指向对象的指针作为参数传递给函数,然后函数通过指针间接分配给对象。

但是,如果您要这样做,您应该更喜欢使用引用而不是指针,这样您就不必处理有人将 null 传递给函数的可能性。

在大多数情况下,最好的方法是 return 一个 class 的实例。即使您只能 return 一个对象,一个 class 也可以有多个子对象。例如,如果你想 return 两个坐标变成欧氏 space,你可以这样做:

struct Point {
    double x;
    double y;
};

Point example()
{
    // until C++20
    return {
        42.,
        1337.,
    };
    // since C++20
    return {
        .x = 42.,
        .y = 1337.,
    };
}

在这里,我们return在一个对象中编辑了两个值。

至少有三种方法。

您已经提到的第一个是 return 使用指针类型的参数的值。

例如

#include <iostream>

int minmax( int x, int y, int *max )
{
    int min = x;
    *max = y;

    if ( y < x )
    {
        min = y;
        *max = x;
    }

    return min; 
}

int main() 
{
    int min, max;

    min = minmax( 20, 10, &max );

    std::cout << "min = " << min << ", max = " << max << '\n';
}

程序输出为

min = 10, max = 20

这里在程序中变量max在C语言中是通过引用传递的

第二种方法是在C++中通过引用传递。例如

#include <iostream>

int minmax( int x, int y, int &max )
{
    int min = x;
    max = y;

    if ( y < x )
    {
        min = y;
        max = x;
    }

    return min; 
}

int main() 
{
    int min, max;

    min = minmax( 20, 10, max );

    std::cout << "min = " << min << ", max = " << max << '\n';
}

程序输出同上图

您确实可以 return 一个对象,但它是包含其他子对象的复合类型。例如

#include <iostream>
#include <utility>

std::pair<int, int> minmax( int x, int y )
{
    return y < x ? std::pair<int, int>( y, x ) : std::pair<int, int>( x, y );
}

int main() 
{
    auto p = minmax( 20, 10 );

    std::cout << "min = " << p.first << ", max = " << p.second << '\n';
}

程序输出也与第一个演示程序所示相同。

您可以使用 class 模板 std::tuple 而不是具有两个数据成员的标准 class 模板 std::pair 如果您想 return 更多而不是两个对象。或者您可以将自己的复合类型(模板或非模板)编写为结构或 class。

例如

#include <iostream>
#include <tuple>

std::tuple<int, int, int> get_sorted( int x, int y, int z )
{
    if ( not ( y < x ) && not ( z < x ) )
    {
        return not ( z < y ) ? std::make_tuple( x, y, z )
                             : std::make_tuple( x, z, y );
    }
    else if ( not ( z < y ) )
    {
        return not ( z < x ) ? std::make_tuple( y, x, z )
                             : std::make_tuple( y, z, x );
    }
    else
    {
        return not ( y < x ) ? std::make_tuple( z, x, y )
                             : std::make_tuple( z, y, x );
    }
}

int main() 
{
    auto t = get_sorted( 15, 10, 20 );

    std::cout << "( " << std::get<0>( t ) 
              << ", " << std::get<1>( t )
              << ", " << std::get<2>( t )
              << " )\n";
}

程序输出为

( 10, 15, 20 )