gpt4 book ai didi

c++ - 对象字段返回对象本身,而不是存储在字段中的实例

转载 作者:行者123 更新时间:2023-11-30 04:29:13 25 4
gpt4 key购买 nike

(不确定这个标题是否是最好的描述...)

大家好。我需要以下代码的帮助。在 process 函数中,我希望 cccc_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;
:
}

最佳答案

您看到的奇怪行为是由于操纵临时 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;
}

关于c++ - 对象字段返回对象本身,而不是存储在字段中的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com