简体   繁体   中英

How to pass member of structure/union array into function

For example, I want to display value of only one member of structure/union array, so I want to pass it as argument into a function that will display only that one member and will also display any other single member that I pass as argument.

#include <iostream>
using namespace std;

union ThreeTypes
{
    char letter;
    int whole;
    double real;
};

void showArr(ThreeTypes[], int); // ?? What parameters to pass?

int main()
{
    const int SIZE = 50;
    ThreeTypes arr[SIZE];
    for (int i = 0; i < SIZE; i++)
        arr[i].real = 2.37;

    showArr(arr, SIZE, ??? ); // what argument to pass to display member?

    return 0;
}
void showArr(ThreeTypes arr[],int size,???) // also, what parameters??
{
    for (int i = 0; i < size; i++)
        cout << arr[i].?? << endl; // member from argument????
}

One option would be a templated pointer-to-member type . These are usually used with class or struct types, but are also valid with a union type. A pointer-to-member type is declared like MemberType ClassType::*pointer_name , and the name of such a pointer can be used to the right of the .* or ->* operator.

template <typename T>
void showArr(const ThreeTypes arr[], int size, T ThreeTypes::*ptr)
{
    for (int i = 0; i < size; ++i)
        std::cout << (arr[i].*ptr) << std::endl;
}

And you create a pointer-to-member value with the syntax &ClassType::member_name :

int main()
{
    const int SIZE = 50;
    ThreeTypes arr[SIZE];
    for (int i = 0; i < SIZE; i++)
        arr[i].real = 2.37;

    showArr(arr, SIZE, &ThreeTypes::real);
}

Another more general option would be to take a callable functor:

template <typename F>
void showArr(const ThreeTypes arr[], int size, const F& func)
{
    for (int i = 0; i < size; ++i)
        std::cout << func(arr[i]) << std::endl;
}

You can create a functor to access a member using a lambda or std::mem_fn :

void print_reals_twice(const ThreeTypes arr[], int size)
{
    showArr(arr, size, [](const ThreeTypes& u) { return u.real; });
    // Same effects:
    showArr(arr, size, std::mem_fn(&ThreeTypes::real));
}

But defining showArr this way also lets you pass a functor that does something more complicated than just return a member, if you wanted:

void print_sin2x_all(const ThreeTypes arr[], int size)
{
    showArr(arr, size, [](const ThreeTypes& u) { return std::sin(2*u.real); });
}

In C++17 you should use std::variant :

using ThreeTypes = std::variant<char, int, double>;

template <std::size_t N>
auto show_arr(std::array<ThreeTypes, N>& arr)
{
    for (auto& e : arr)
    {
        std::visit([](auto e) { std::cout << e << std::endl;}, e);
    }
}

auto test()
{
    std::array<ThreeTypes, 2> arr = {3.4, 'a'};

    show_arr(arr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM