返回一个新对象以及另一个值

发布于 2024-07-24 23:59:17 字数 892 浏览 7 评论 0原文

我想返回两个值,其中之一是一个新对象。 我可以使用 std::pair 来做到这一点:

class A {
//...
};

std::pair<A*, int> getA()
{
   A* a = new A;
   //...
} 

为了使代码异常安全,我想做:

std::pair<std::auto_ptr<A>, int> getA()
{
    std::auto_ptr<A> a(new A); 
    //...
} 

但这不会编译,因为无法复制 auto_ptr无需修改正在复制的 auto_ptr 。 好吧,这意味着 auto_ptr 不像其他类型那样很好地组合(以另一种方式)。 在这种情况下返回新对象的好方法是什么?

一种替代方法是返回 shared_ptr,另一种方法是返回 inout 引用。 但我正在寻找其他替代方案。 我可以做这样的事情:

class AGetter
{
    void getAExecute()
    {
         //...
         // set a_ and i_
    }
    std::auto_ptr<A> getA() const
    {
         return a_.release();
    }
    int getInt() const
    {
         return i_;
    }
    private:
    std::auto_ptr<A> a_;
    int i_;
};

有更好的方法吗?

I want to return two values, one of which is a new object. I can do this using std::pair:

class A {
//...
};

std::pair<A*, int> getA()
{
   A* a = new A;
   //...
} 

To make the code exception-safe, I would like to do:

std::pair<std::auto_ptr<A>, int> getA()
{
    std::auto_ptr<A> a(new A); 
    //...
} 

But this won't compile as the auto_ptr cannot be copied without modifying the auto_ptr being copied. Ok, this means auto_ptr does not compose well like other types (in one more way). What is a good way of returning a new object in this case?

One alternative is to return a shared_ptr and another is an inout reference. But I am looking for some other alternative. I can do something like:

class AGetter
{
    void getAExecute()
    {
         //...
         // set a_ and i_
    }
    std::auto_ptr<A> getA() const
    {
         return a_.release();
    }
    int getInt() const
    {
         return i_;
    }
    private:
    std::auto_ptr<A> a_;
    int i_;
};

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天冷不及心凉 2024-07-31 23:59:17

有两种主要方法可以处理此问题:

  1. 通过 try/catch 自行清理。
  2. 使用某种自动内存管理,例如shared_ptr

正如您所发现的那样,auto_ptr 在这种情况下实际上不起作用,但新标准和 Boost 都包含可以执行您想要的操作的引用计数指针。 请告诉我们您不喜欢 shared_ptr 的哪些方面,也许我们可以建议替代方案。

There are two major ways to handle this problem:

  1. Do the cleanup yourself, via try/catch.
  2. Use some kind of automated memory management, like shared_ptr.

auto_ptr doesn't really work in these sorts of cases, as you discovered, but the new standard and Boost both contain reference-counted pointers that do what you want. Let us know what you don't like about shared_ptr, and maybe we can suggest an alternative.

请止步禁区 2024-07-31 23:59:17

在这种情况下,shared_ptr 是理想的选择,但如果您确实不想使用它们,则可以将 auto_ptr 返回到包含对象和 int 的对。

A shared_ptr would be ideal in that situation, but if you really don't want to use those you could return an auto_ptr to a pair containing the object and the int instead.

无妨# 2024-07-31 23:59:17

只需创建一个新类并返回该类

class Result
{
   A* a;
   int i;
public:
   Result( A*a, int i ) : a(a), i(i) {
   }
   ~Result() { 
      delete a; 
   }
   // access functions, copy constructor, ...
};

Result getA() {
   //...    
   return Result(new A, intValue);  
}

Just create a new class and return that class

class Result
{
   A* a;
   int i;
public:
   Result( A*a, int i ) : a(a), i(i) {
   }
   ~Result() { 
      delete a; 
   }
   // access functions, copy constructor, ...
};

Result getA() {
   //...    
   return Result(new A, intValue);  
}
嘿嘿嘿 2024-07-31 23:59:17

它并不漂亮,但您可以通过指针或引用参数返回另一个值:

int i;
std::auto_ptr<A> a = getA(&i);

It's not pretty, but you could return the other value through a pointer or reference argument:

int i;
std::auto_ptr<A> a = getA(&i);
梦断已成空 2024-07-31 23:59:17

为什么不通过两个参考参数呢?

class AGetter
{
    // ..
    void get(std::auto_ptr<A>& a, int& i)
    {
         a = a_.release();
         i = i_;
    }
    // ..
};

Why not through two reference parameters?

class AGetter
{
    // ..
    void get(std::auto_ptr<A>& a, int& i)
    {
         a = a_.release();
         i = i_;
    }
    // ..
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文