具有责任转移的转换构造函数 [英] Conversion constructor with responsibility transfer
问题描述
这是此问题的后续行动。
我需要执行一个转换操作,以将成员对象的责任从一个对象传递到另一个对象。转换操作在类层次结构中进行。有一个 Base
类和两个派生类,例如 Child1
和 Child2
。在 Base
类中,有一个动态创建的对象,我需要从 Child1
传递到在进行转换时,Child2
(以及责任)不会让 Child1
的析构函数销毁它。我已经写了一个简单的示例来说明我要实现的目标:
I need to implement a conversion operation to pass responsibility for a member object from one object to another. The conversion operation takes place in a class hierarchy. There is a Base
class and two deriving classes, say Child1
and Child2
. In the Base
class, there is a dynamically created object I need to pass from Child1
to Child2
(along with responsibility) while the conversion happens and not let Child1
's destructor destroy it. I've written a simple example to illustrate what I'm trying to achieve:
#include <iostream>
using namespace std;
class Base {
public:
Base() {
p_int = new int; *p_int = 0;
cout << "In Base constructor, reserving memory." << endl;
}
Base(const Base& other) : p_int(other.p_int), a(other.a) {
cout << "In Base copy-constructor." << endl;
}
virtual ~Base() { delete p_int; p_int = NULL; cout << "Freeing memory." << endl; }
void setpInt(int val) { *p_int = val; }
void setInt(int val) { a = val; }
virtual void print() {
cout << "Base: ";
cout << (long)p_int << ":" << *p_int << " " << a << endl;
}
protected:
int* p_int;
int a;
};
class Child1 : public Base {
public:
Child1() : Base() {};
Child1(const Base& base) : Base(base) {}
void print() {
cout << "Child1: ";
cout << (long)p_int << ":" << *p_int << " " << a << endl;
}
};
class Child2 : public Base {
public:
Child2() : Base() {};
Child2(const Base& base) : Base(base) {}
void print() {
cout << "Child2: ";
cout << (long)p_int << ":" << *p_int << " " << a << endl;
}
};
int main() {
Child1* c1 = new Child1();
c1->setpInt(3);
c1->setInt(2);
c1->print();
Child2* c2 = new Child2(*c1);
c2->print();
delete c1; //Obviously c1's destructor is called here.
c2->print();
delete c2;
return 0;
}
结果为:
In Base constructor, reserving memory.
Child1: 158711832:3 2
In Base copy-constructor.
Child2: 158711832:3 2
Freeing memory.
Child2: 158711832:0 2
Freeing memory.
有没有一种方法可以干净地做我想做的事情?我无法复制构造 p_int
,因为它很重。该项目是一个嵌入式AVR项目,因此可能无法使用智能指针或Boost库(尽管我不知道)-我只是记得这可能是一种解决方案,但我从未使用过它们。 / p>
Is there a way to do what I'm trying to do in a clean way? I can't copy-construct p_int
because it's very heavy. The project is an embedded, AVR-project, so smart-pointers or boost library might not be available (I don't know, though) - I just recall this might be a solution, but I've never used them yet.
推荐答案
我认为您需要查看引用计数对象。本质上,对象本身会跟踪其使用情况,而不是使用指向引用的指针来存储指向该对象的原始指针。
I think you need to look at reference-counted objects. Essentially, the object itself tracks it's usage itself, and instead of storing a raw pointer to the object, you use a reference-counted pointer.
Scott Meyers在这里谈到:
Scott Meyers talks about this here:
http://www.aristeia .com / BookErrata / M29Source.html
在嵌入式系统中,我不确定您是否可以使用 boost :: shared_ptr<>
,但是没有什么可以阻止您按照Meyers的概述来实现。
As you're on an embedded system, I'm not sure you can use boost::shared_ptr<>
but there's nothing stopping you implementing this as Meyers outlines.
因此,与其原始指向在基类中有问题的对象具有共享的指针/引用计数的指针,一旦将其复制到 Child2
,它将防止删除该对象。按照Child2的构造,它将有2个引用,因此删除Child1时不会死亡。但是,当删除Child2时,它将自动删除。
So, instead of having a raw pointer to the object in question in the base class, have a shared pointer/reference-counted pointer that will prevent the deletion of the object once it gets copied in to Child2
. By the construction of Child2, it will have 2 references so won't die when Child1 gets deleted. It will, however, when Child2 is deleted.
这篇关于具有责任转移的转换构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!