函数可以返回不同的类型? [英] function which is able to return different types?

查看:81
本文介绍了函数可以返回不同的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在c ++中创建一个函数,我想知道是否可以创建它以使其能够返回不同类型的向量。例如,根据不同的情况,它返回向量字符串,int,double或任何东西。
在c ++中可能吗? (我不想使用具有不同arg(S)和不同返回值的重载函数)
我对C ++非常陌生,我的问题似乎很愚蠢。

I am trying to create a function in c++, I am wondering if I can create it such that it is able to return different types of vectors. e.g based on different case it returns vector string, int, double or ...anything. Is it possible in c++? (I do not want to use overload function with different arg(S) and different returns) I am very new to C++ and my question may seem to be stupid.

这是我的一段代码:

//这里的零表示交集

//zero here means intersection

std::vector<??????> findZeros(const mesh::Region& s, char *model) const
{
//Point  
  if( model == "point" )
  {
    std::vector<Vertex> zeros;
    for(Region::pointIterator it = s.beginPoint(); itv != s.endPoint(); ++itv )
    {
      if( abs(Val(*it)) < 1.e-12 )      
      zeros.push_back(*it);
    }
    std::vector<point> zerosP(zeros.begin(), zeros.end());
    return zerosP;
  }
 //line
  else if (EntityS == "line")
  {
    std::vector<line> zerosE;
    std::vector<Point&> PointE;
    for(Region::lineIterator ite = s.beginLine(); ite != s.endLine(); ++ite )
    {
      Line ed = *ite;
        Point P0 = ed.point(0);
        Point P1 = e.point(1);
        if( ......... ) zerosE.push_back(ed);
        else if ( ....... )
        {
         PontE.push_back( P0, P1);
         zerosE.push_back(ed);
        }
      }

//在这里我想返回点或与它的点成一直线或在表面上。
//我想做一个功能!
}

//here I want to return "point" or "line with its points" or in upper level our surface. //I want to do all in one function! }

推荐答案

模板



尝试以下操作:

Templates

Try this:

template <typename T>
std::vector<T> func( /* arguments */ )
{
    std::vector<T> v;
    // ... do some stuff to the vector ...
    return v;
}

您可以通过以下方式调用此函数:

You can call this function with different type in this way:

std::vector<int> func<int>( args );
std::vector<double> func<double>( args );



替代方案



这是一种方式,如果您在编译时知道类型。如果您在编译时不知道类型,而仅在运行时知道,则您有不同的选择:

Alternatives

This is one way, if you know the types at compile-time. If you don't know the type at compile-time but at run-time only, then you have different choices:


  1. 使用工会。如果您具有非常简单的类似于C结构的类型,而在C ++标准中称为POD(普通旧数据),则我只能建议这样做。

  2. 使用某种类型的变体。例如,有 boost :: variant 从Boost库或 QVariant 来自Qt库。在更一般的类型上,它们是安全的工会。它们还允许在不同类型之间进行一些转换。例如,将某物设置为整数值将可以读取与浮点数相同的值。

  3. 使用 boost: :any 可以包装任何类型,但不允许它们之间进行转换。

  4. 使用继承和多态性。对于这种情况,您需要一个通用的基类,例如 Base 。然后,最好使用 std :: shared_ptrs 创建一个指向该基址的指针数组。因此,数组类型将为 std :: vector< std :: shared_ptr< Base>> 。在这种情况下, std :: shared_ptr 比内置指针要好,因为可以通过引用计数自动管理内存。

  5. 使用不关心类型和性能的动态语言。

  1. Use unions. I can only recommend this, if you have very simple C-struct-like types which are called PODs (plain old data) in the C++ standard.
  2. Use some type of variant. For example there is boost::variant from the Boost libraries or QVariant from the Qt library. They are a safe kind of unions on more general types. They also allow some conversions between different types. For example setting something to an integer value will make it possible to read the same value as floating point number.
  3. Use boost::any which can wrap any type but does not allow conversions between them.
  4. Use inheritance and polymorphism. For this case you need a common base class, say Base. Then you create an array of pointers to that base preferably with std::shared_ptrs. So the array type would be std::vector<std::shared_ptr<Base>>. The std::shared_ptr is better than built in pointers in this case because the manage your memory automagically by reference counting.
  5. Use a dynamic language that doesn't care about types and performance.

这篇关于函数可以返回不同的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆