(不确定这个标题是否是最佳描述...)
大家好。我需要以下代码的帮助。在process
函数中,我希望cc
和cc_sub
(作为cc
中的字段获得)显示不同的内容(因为它们最初是这样做的),但它们显示相同。 Storage
个实例在makeObj
函数中生成。有人能告诉我什么是错的以及如何解决这个问题?谢谢。
Int process(const Storage cc) {
Storage cc_sub = (Storage&) cc.data; // Here
cout << ... << endl; // Contents from cc and cc_sub are the same though they are not supposed to be.
}
class Storage {
public:
Storage() {
this->data = NULL;
}
Storage(const Storage& obj) { //Copy Constructor. Result is the same even if I removed this entire clause.
this->data = obj.data;
}
void* data;
void setData(Storage dPtr) {
this->data = &dPtr;
}
};
Storage makeObj(std::string* s) { // I want to keep the return value as value instead of pointer,
// since when it returns pointer the values pointed by reference behaved unexpectedly (guess it's because of temporary object type of error)...
Storage cc;
:
cc.setData(makeObj(&subString));
return cc;
:
}
答案 0 :(得分:2)
您看到的奇怪行为是由于操纵Storage
和setData
内的临时makeObj
变量。这种操作的结果在C ++中是未定义的。
尝试以下方法:
void process(const Storage cc) {
Storage cc_sub = *((Storage*)cc.data);
cout << ... << endl;
}
class Storage {
public:
Storage() {
this->data = NULL;
}
Storage(const Storage& obj) {
this->data = obj.data;
}
void* data;
void setData(Storage* dPtr) {
this->data = dPtr;
}
};
Storage* makeObj(std::string* s) {
// Allocate memory on the heap so that cc lives beyond this function call
Storage* cc = new Storage;
:
// Populate the sub-storage with a heap-allocated variable
cc->setData(makeObj(&subString));
return cc;
}
注意,当前编写的代码makeObj
会因为无法结束递归而导致堆栈溢出...
对于cHao的建议,这是一个使用shared_ptr
的更完整的程序(Microsoft Visual Studio 2008版本 - 根据您的编译器,您可能需要更改#include <memory>
和/或std::tr1::shared_ptr
。我已将数据类型从void*
更改为Storage
(shared_ptr
),并添加了一个字符串成员以更好地显示差异:
#include <string>
#include <iostream>
#include <memory>
using namespace std;
using namespace std::tr1;
class Storage {
public:
Storage() {
}
Storage(const Storage& obj) {
this->data = obj.data;
}
string s;
shared_ptr<Storage> data;
void setData(shared_ptr<Storage> dPtr) {
this->data = dPtr;
}
};
void process(const shared_ptr<Storage> cc) {
shared_ptr<Storage> cc_sub = cc.get()->data;
cout << "Parent string: " << cc.get()->s << endl <<
"Child string: " << cc_sub.get()->s << endl;
cout << "Parent data pointer: " << cc.get()->data << endl <<
"Child data pointer: " << cc_sub.get()->data << endl;
}
shared_ptr<Storage> makeObj(string* s) {
shared_ptr<Storage> cc(new Storage);
if (s->length() != 0) {
string subString = s->substr(0, s->length()-1);
cc->s = subString;
cc->setData(makeObj(&subString));
}
return cc;
}
int main(int argc, char* argv[]) {
string s = "hello";
shared_ptr<Storage> storage = makeObj(&s);
process(storage);
return 0;
}
答案 1 :(得分:0)
在我开始之前:你想用(Storage &)
演员实现什么目标?我不认为我知道语法。我会尽量使用我对你正在做的事情的最接近的估计。
编辑:我的答案的这一部分已经过时,请参阅下面的评论。
执行此操作时:
Storage cc_sub = *(Storage *) cc.data;
您正在使用指针
cc.data
,将其转换为Storage
和 将它分配给变量cc_sub。复制构造函数几乎没有 有了这个。尝试覆盖赋值运算符并查看是否存在 帮助。或者尝试:Storage cc_sub(*(Storage*) cc.data);
进行显式复制构造函数调用。
如果它仍然无效,则可能与您的程序逻辑有关,因为我看到您正在以Storage
成员递归引用您的data
个实例...(毕竟,如果您的cc.data
指向cc
,那么行为是否正确,不是吗?)