简体   繁体   English

如何从 c++ 中的 function 返回不同的类型?

[英]how return different types from function in c++?

I have a homework in c++ to make a Vector for multi data types in the same vector, I stuck where I need to return different data types and I can't change the main.我在 c++ 中有一个作业,为同一个Vector中的多种数据类型制作一个向量,我卡在需要返回不同数据类型的地方,我无法更改主。

The homework ask to make the main valid:作业要求使主要有效:

int main()
{
    Vector v;
    v.add(5);
    v.add(5.5f);
    v.add("this");
    for (int i = 0; i != v.size(); ++i) { // this code print 5 5.5 this
        std::cout << v.get(i) << "\t";
    }
    std::cout << "Displaying an object of type Vector in different way:\n";
    Integer x;
    x = v.get(0);
    std::cout << x << "\t"; // it prints 5
    Float y;
    y = v.get(1);
    std::cout << y << "\t"; // it prints 5.5
    String z;
    z = v.get(2);
    std::cout << z << "\t"; // it prints this
}

I tried what I know and I got 3 ways but still didn't fix that first I made all data pointer is char* and made a string type that worked with saving the data and cout but it stuck on return data type, I cant use template because I'm not allowed to change the main我尝试了我所知道的,我得到了 3 种方法,但仍然没有解决这个问题,首先我将所有数据指针设为 char*,并创建了一个字符串类型,用于保存数据和 cout,但它停留在返回数据类型上,我无法使用模板,因为我不允许更改主

int get(int n)
{
    Node* p = head;
    int i;
    for (i = 0; (i < n)&&p->next; i++)
        p = p->next;
    if (i != n)
        return NULL;
    if (p->type != "int")
        return NULL;
    else return *((int*)p->data);
}

still I cant overload functions by just the return type, I tried to make vector have 3 pointers to data but still I stuck我仍然不能仅通过返回类型重载函数,我试图让向量有 3 个指向数据的指针,但我仍然卡住了

int* int_;
float* float_;
string* string_;
Node* next;
string type;
Node(string type_)

I searched on internet and still not found what I want, at last I tried to make the Node template but since the get function is on the vector and the main didn't send a type that's didn't solve the problem.我在互联网上搜索,仍然没有找到我想要的,最后我尝试制作节点模板,但是由于 get function 在矢量上,并且主要没有发送没有解决问题的类型。

You might return wrapper with conversion operator, something like:您可以使用转换运算符返回包装器,例如:

struct data
{
    int int_ = 0;
    float float_ = 0;
    string string_;

    operator int () const { return int_; }
    operator float () const { return float_; }
    operator std::string () const { return string_; }
};


const Node* get_node(int n) const
{
    Node* p = head;
    int i;
    for (i = 0; (i < n) && p->next; i++)
        p = p->next;
    return p;
}

data get(int n) const
{
    auto* node = get_node(n);
    if (node == nullptr) throw std::out_of_range{};
    return node->data;
}

Demo演示

If I understood correctly, what you are looking for is an heterogeneous container .如果我理解正确,您正在寻找的是一个异构容器

You need to create a wrapper that handle different types transparently inside your container.您需要创建一个包装器,在容器内透明地处理不同类型。 For your underlying type you have several options depending on your necessity.对于您的基础类型,您有多种选择,具体取决于您的需要。

If you know upfront the list of types that you need to support at compile type, you can use std::variant https://en.cppreference.com/w/cpp/utility/variant .如果您预先知道需要在编译类型时支持的类型列表,则可以使用 std::variant https://en.cppreference.com/w/cpp/utility/variant

If the types that you need to support are unbounded at compile time you can use https://en.cppreference.com/w/cpp/utility/any如果您需要支持的类型在编译时不受限制,您可以使用https://en.cppreference.com/w/cpp/utility/any

Finally, you can create a common interface and implement a wrapper for your supported types.最后,您可以创建一个通用接口并为您支持的类型实现一个包装器。 The container will store a smart pointer of your objects.容器将存储对象的智能指针。

UPDATE更新

  • removed suggestion on designed decision based on the C++ version删除了基于 C++ 版本的设计决策建议
  • removed reference to std::vector删除了对 std::vector 的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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