English 中文(简体)
标明其他物体的物体的深厚副本
原标题:deep copy of object which hold references to other objects

我有一个“假设”类别,有两处提及现有斜体。 我想制造一种“影印”方法,深入复制这些墨迹。 我认为我永远不必人工操作<代码>delete。 在我的法典中,由于聪明才智者,我不得不在这种解决办法中提出反对。 此外,这项任务过于复杂(我需要重复几个班)。 是否有更直截了当的解决办法?

注:我不想在每一物体中增加一个ool子(flag)成员,以确定必须删除ts(就我而言,它不比<代码>std:<>/code>:> 司机检查费用

#include <set>

struct sum {
   const int &a, &b;
   static std::set<const int*> allocated_ints;

   sum(const int& a, const int&b): a(a), b(b) {}

   sum copy() const {
       sum res(*new const int(a), *new const int(b));
       allocated_ints.insert(&res.a);
       allocated_ints.insert(&res.b);
       return res;
   }

   ~sum() {
       if (allocated_ints.count(&this->a)) {
           delete &this->a;
           delete &this->b;
           allocated_ints.erase(&this->a);
           allocated_ints.erase(&this->b);
       }
   }

};

std::set<const int*> sum::allocated_ints;
问题回答

什么是constants的“deep”拷贝? 不管怎么说,这些常数将具有同样的价值。 因此,仅复制(即别的)参考文献:

struct Foo
{
  const int & n;

  Foo(const int & m) : n(m) { }
  Foo(const Foo & rhs) : n(rhs.n) { }

  Foo copy() const { Foo f(*this); /* ... */ return f; }
  // ...
};

If you re worried about dangling references when returning a copy from a function with a reference to a local variable, then don t make the class have const references, but copies. That way you naturally give your class the copy semantics that you seem to be after anyway.

如果你认为你可以做一种不拥有的混合体,或者视你如何使用它而自行拥有,那么,我就说,你应避免设计错误。 2. 决定贵阶层是否拥有数据,然后在数据上进行。

我认为,你重新组合了两个不相容的概念。

如果您先提出申请,你就应当提及其寿命已经确定并且应当比您的目标更长的现有物体。

如果你想制作贵国物体的复制件,因为refers to,则您的复印件也将提到:

如果你想要拥有能动供应的物体,你就应当为此使用点子,并作为点子(在销毁时删除)。 然后,一份复印件可以复制已标明物体的深重拷贝(或可使用参考计数或共享接收器分享)。

你们事实上是两件事情的组合,产生了可能的问题:

int main()
{
    const int x=5; //whatever it is
    Foo foo(x);
    // ...
} //danger here! ~Foo() will delete x

这些提法没有被深入复制,因为它们指物体。 因此,您的法典应当这样考虑:

struct sum {
   const int &a, &b;

   sum(const int& a, const int&b): a(a), b(b) {}

   sum copy() const {
       sum res(a,b);
       return res;
   }

   ~sum() {
   }

};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签