用于传递值的返回类型多态性 [英] Return-type polymorphism for pass-by-value

查看:135
本文介绍了用于传递值的返回类型多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定问题标题是否准确...让我先解释一下我原来的简单方案,然后继续解释我想做什么,但不能。



最初,我有类似的东西:

  class Operand; 

操作数genOperandA(){...; return Operand(); }
Operand genOperandB(){...; return Operand(); }
... //更多的操作数生成函数

typedef Operand(* OpGen)();

//函数指针表
static const OpGen生成器[] =
{
genOperandA,
genOperandB,
...
};

//对操作数做一些操作的函数
void operators(Operand&op;);

...

//调用示例
操作(生成器[1]());

到目前为止很好(我想)。然而,现在有几种导出的操作数类型,例如。 class RegisterOperand:public Operand 。我有新的,专门的 genOperand 函数,理想情况下将返回派生类型的实例。但我不能这样做:

  Operand genOperandC(){...;返回RegisterOperand(); } 

我不能这样做:

  RegisterOperand genOperandC(){...; return RegisterOperand(); } 

static const OpGen generators [] =
{
...
genOperandC,
};然而,我知道这将工作,如果我返回引用或指针类型,所以唯一的选项。我现在有类似:

  Operand * genOperandC(){...; return new RegisterOperand(); } 

现在需要显式清理,最初不需要。



我没有考虑过的任何替代方案?

解决方案






如果返回一个指针,指针是一个问题(因为需要清理的东西),你一定应该考虑使用智能指针作为返回类型。



具有智能指针的工厂方法:

  boost :: shared_ptr< Operand> genOperandC()
{
return boost :: shared_ptr< Operand>(new RegisterOperand());
}

这样,你不必调用删除手动:它将由 boost :: shared_ptr< Operand> 的析构函数来完成。



如果之后你需要转换结果指针, boost 也提供转换函数:

  boost :: shared_ptr< Operand> op = genOperandC(); 

boost :: shared_ptr< RegisterOperand> rop =
boost :: dynamic_pointer_cast< RegisterOperand>(op);


I'm not sure if the question title is accurate... Let me start by explaining my original simple scenario, and then move on to explain what would I like to do, but can't.

Originally, I had something like:

class Operand;

Operand genOperandA() { ...; return Operand(); }
Operand genOperandB() { ...; return Operand(); }
... // more operand-generation functions

typedef Operand (*OpGen)();

// Table of function pointers
static const OpGen generators[] =
{
    genOperandA,
    genOperandB,
    ...
};

// Function to do some operation on the operand
void operate(Operand& op);

...

// Example call
operate(generators[1]());

So far so good (I think). However, there are now several derived operand types, e.g. class RegisterOperand : public Operand. I have new, dedicated genOperand functions that ideally would return instances of the derived types. But I can't do this:

Operand genOperandC() { ...; return RegisterOperand(); }

and I can't do this:

RegisterOperand genOperandC() { ...; return RegisterOperand(); }

static const OpGen generators[] = 
{
    ...
    genOperandC,
};

However, I know this would work if I were to return reference or pointer types, so the only option I currently have is something like:

Operand *genOperandC() { ...; return new RegisterOperand(); }

which now requires explicit cleanup which wasn't necessary originally.

Any alternatives I haven't considered?

解决方案

There might be other designs that doesn't require you to use pointers, but if you need or want to go this way, this might interest you.


If returning a pointer is a problem (because of the need to "clean-up" things), you definitely should consider using smart pointers as return type.

Here is an example of your factory method with smart pointers:

boost::shared_ptr<Operand> genOperandC()
{
  return boost::shared_ptr<Operand>(new RegisterOperand());
}

This way, you won't have to call delete manually: it will be done by the destructor of boost::shared_ptr<Operand> for you when required.

If afterwards you need to cast the resulting pointer, boost provides casting functions as well:

boost::shared_ptr<Operand> op = genOperandC();

boost::shared_ptr<RegisterOperand> rop =
  boost::dynamic_pointer_cast<RegisterOperand>(op);

这篇关于用于传递值的返回类型多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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