C ++获取对返回对象的引用 [英] C++ obtaining a reference to a returned object

查看:260
本文介绍了C ++获取对返回对象的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过引用获取对象,我习惯这样做:

  MyObject& obj = FactoryThatGivesAnObject(); 
obj.MethodThatModifieObj();

不需要根据条件:

  MyObject obj; 

//需要obj作为下面返回值的引用
if(foo)
obj = FactoryThatGivesAnObject();
else
obj = OtherFactoryThatGivesAnObject();

obj.MethodThatModifiesObj();

如何在第二个示例中使obj成为引用?

解决方案

与指针不同,引用只能设置一次。这是一个有用的功能很多次,但这是一个令人沮丧的方面。



您有两个选项。



1)使用三元运算符



这通常是最简单的,如果你只处理两个工厂,和一个简单的布尔值来决定使用:

  MyObject& obj =(foo 
?FactoryThatGivesAnObject();
:OtherFactoryThatGivesAnObject());但是,如果 foo 更复杂,



2)使用您自己的工厂方法

  MyObject& get_an_object(const int state)//或任何你需要的参数
{
switch(state)
{
case USE_LEGACY_FACTORY:return FactoryThatGivesAnObject();
case USE_FOO_FACTORY:return OtherFactoryThatGivesAnObject();
case DO_SOMETHING_ELSE:return YetAnother ObjectFactory();
}
throw std :: runtime_error(Bad Factory Selector);
}

//用法更简单
MyObject& obj = get_an_object(foo);

请注意,您可能需要向工厂方法传递几个参数:




  • 选择条件。您的示例只是 foo - 一个简单的布尔值。随着事情的增长,您可能需要其他条件来帮助确定要使用哪个工厂。

  • 工厂对象。您可能有工厂对象,而不是工厂方法,在这种情况下,您需要将这些对象的引用传递到您的方法中。


I need to grab an object by reference and I used to do it like so:

MyObject& obj = FactoryThatGivesAnObject();
obj.MethodThatModifieObj();

No I need to do it based on a conditional:

MyObject obj;

// Need obj to be a reference to the returned values below
if( foo )
    obj = FactoryThatGivesAnObject();
else
    obj = OtherFactoryThatGivesAnObject();

obj.MethodThatModifiesObj();

How can I have obj be a reference in the second example?

解决方案

References, unlike pointers, can only be set once. This is a useful feature many times, but this is the one frustrating aspect about it. You only want to set the reference once, but possibly to different things.

You have two options.

1) Use the ternary operator

This is often the easiest, if you're only dealing with two factories, and a simple boolean to decide which to use:

MyObject& obj = ( foo
                  ? FactoryThatGivesAnObject();
                  : OtherFactoryThatGivesAnObject() );

However, if foo is more complicated, or if you have multiple factory options, the next option may be cleaner.

2) Use a factory method of your own

MyObject& get_an_object(const int state) // or whatever parameters you need
{
    switch(state)
    {
       case USE_LEGACY_FACTORY:    return FactoryThatGivesAnObject();
       case USE_FOO_FACTORY:       return OtherFactoryThatGivesAnObject();
       case DO_SOMETHING_ELSE:     return YetAnotherObjectFactory();
    }
    throw std::runtime_error("Bad Factory Selector");
}

// usage is simpler now
MyObject& obj = get_an_object(foo);

Note that you may need to pass several parameters to your factory method:

  • selection criteria. Your example was just foo - a simple boolean. As things grow, you may need additional criteria to help determine which factory to use.
  • factory objects. You may have factory objects instead of factory methods, in which case you need to pass references to those objects into your method.

这篇关于C ++获取对返回对象的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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