在c ++类中的可变返回类型 [英] variable return type in c++ class

查看:129
本文介绍了在c ++类中的可变返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其结构如下:

i have a class with the following structure:

class myClass
{
    private:
        int type;
        classOne objectOne;
        classTwo objectTwo;
    public:
        myClass(classOne object)
        {
            this->objectOne = object;
            this->type = 0;
        }
        myClass(classTwo object)
        {
            this->objectTwo = object;
            this->type = 1;
        }
}



现在我想要一个方法返回类型 classOne 如果类型是0,并且类型 classTwo 如果类型是1.我不想要两个方法来实现。这些类有不同的结构。

i now want a method returning an object of type classOne if type is 0 and of type classTwo if type is 1. I do not want two methods to achieve this. the classes have different structures.

这是可能吗?任何建议:)

Is this even possible? Any suggestions are appreciated :)

推荐答案

您可以使用 Boost.Variant 来执行此操作。变量可以直接从可转换为其有界类型之一的任何值构造。类似地,可以向变体分配可转换为其有界类型之一的任何值。您可以在课堂上使用它:

You can use Boost.Variant to do this. A variant can be constructed directly from any value convertible to one of its bounded types. Similarly, a variant can be assigned any value convertible to one of its bounded types. Heres how you could use it in your class:

class myClass
{
    private:
        boost::variant<classOne, classTwo> obj;
    public:
        myClass(classOne object) : obj(object)
        {
        }
        myClass(classTwo object) : obj(object)
        {
        }
};

它还提供了一个非常方便的 boost :: get 从变量中检索值。
您可以使用它为每个有界类型提供代码(即 classOne classTwo )。下面是一个例子:

It also provides a very convenient boost::get to retrieve the value from the variant. You can use that to supply code for each bounded type you have(ie classOne and classTwo). Here is an example:

if (classOne * x = boost::get<classOne>(&obj))
{
    //Code for classOne
}
else if (classTwo * x = boost::get<classTwo>(&obj)
{
    //Code for classTwo
}

但是,这样的代码相当脆弱,因此,实际使用变体通常需要一个比 get 更强大的访问机制,因此, variant通过 apply_visitor 支持编译时检查的访问。访问需要程序员显式地处理(或忽略)每个有界类型,否则会导致编译时错误。

However, such code is quite brittle, and without careful attention will likely lead to the introduction of subtle logical errors detectable only at runtime. Thus, real-world use of variant typically demands an access mechanism more robust than get. For this reason, variant supports compile-time checked visitation via apply_visitor. Visitation requires that the programmer explicitly handle (or ignore) each bounded type. Failure to do so results in a compile-time error.

变体的访问需要一个访问者对象。如下:

Visitation of a variant requires a visitor object. Like this:

class object_visitor
: public boost::static_visitor<>
{
public:

    void operator()(classOne & x) const
    {
        //Code for classOne
    }

    void operator()(classTwo & x) const
    {
        //Code for classTwo
    }

};

通过上述访问者的实现,我们可以将它应用到 obj ,如下所示:

With the implementation of the above visitor, we can then apply it to obj, as seen in the following:

boost::apply_visitor( object_visitor(), obj );

这篇关于在c ++类中的可变返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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