简体   繁体   English

如何编写一个返回一个类型的函数,该类型是其子函数的返回类型

[英]how to write a function that return a type which is return type of its sub-functions

I want to write a function that calls several sub functions and return the result of these sub functions. 我想编写一个函数来调用几个子函数并返回这些子函数的结果。

sub functions:
template<class A> A sub1(A a)
template<class B> B sub2(B b, int i)
template<class C> C sub3(C c, string p)

THE function will call these accordingly in the switch statement. 该函数将在switch语句中相应地调用它们。 Sorry I only have pseudo code since I am confused with the issue and not start to write the code. 对不起,我只有伪代码,因为我对这个问题感到困惑而没有开始编写代码。

mf(string s)
{
  int k;
  k = process(s)
  string
  switch (k){
  case 0:
    return sub1(k);
  case 1:
    return sub2(s, k);
  case 2:
    return sub3(k, s);
  default:
    break;
  }
}

How can I define mf above since there is no return type for it now? 我如何定义上面的mf,因为现在没有返回类型? using template again? 再次使用模板? By the way, my c++ compiler does support c++ 11 standard which I am not so familiar with. 顺便说一下,我的c ++编译器确实支持我不太熟悉的c ++ 11标准。

If you need a function that returns the value of its sub function you need the same return type for all of them. 如果需要一个返回其子函数值的函数,则所有函数都需要相同的返回类型。

Here a small meaningless example: 这是一个小小的无意义的例子:

double calculatedPositive(double value)
{
    // Do stuff
}

double calculatedNegative(double value)
{
    // Do stuff
}

double functionA(double value)
{
     if(value > 0)
         return calculatePositive(value);
     else
         return calculateNegative(value);
}

P.-S. P.-S. We could provide you with a better answer if you'd say what you are trying to achieve ;) 如果您说出您想要实现的目标,我们可以为您提供更好的答案;)

C++ is basically a static-typed language, which means all types of expressions are decided at compile time rather than at run time. C ++基本上是一种静态类型语言,这意味着所有类型的表达式都是在编译时而不是在运行时决定的。

Using dynamic-typing in a static-typed language is possible, but not recommended for widely use. 可以在静态类型语言中使用动态类型,但不建议广泛使用。 Because doing so you're giving up almost all the polymorphism features provided by the language. 因为这样做你几乎放弃了语言提供的所有多态性功能。 You'll have to check types manually, or implement your own dynamic-type-based polymorphism. 您必须手动检查类型,或实现自己的基于动态类型的多态。

If the data returned is not too complex, tagged structure is usually a good idea: 如果返回的数据不是太复杂,标记结构通常是个好主意:

struct Value
{
    enum {INT, FLOAT, PTR} type;
    union
    {
        int   int_data;
        float float_data;
        void *ptr_data;
    };
};

For more complex data types with a lot of operations needed to support, you should consider using abstract interfaces and inheritance. 对于需要支持大量操作的更复杂的数据类型,您应该考虑使用抽象接口和继承。

If you considered the problem seriously and believe that none of those methods above applies to your problem, and that dynamic typing is the best way, here are some options: 如果您认真考虑了这个问题,并且认为上述方法都不适用于您的问题,并且动态类型是最佳方式,则可以选择以下方法:

  • boost::any -- A unique container for all types. boost::any - 适用于所有类型的唯一容器。 Need to test for types and convert them manually before use. 需要测试类型并在使用前手动转换它们。

  • boost::variant -- A union-like container which supports unary polymorphic operations via boost::static_visitor . boost::variant - 一个类似union的容器,它通过boost::static_visitor支持一元多态操作。

  • Some programming frameworks have their own support for dynamic-typing. 一些编程框架对动态类型有自己的支持。 One example is QVariant in Qt. 一个例子是Qt中的QVariant If you are in such a framework, it's usually recommended to use them instead of something else from another library. 如果您在这样的框架中,通常建议使用它们而不是其他库中的其他内容。

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

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