gpt4 book ai didi

c++ - 从对象外部操作链表

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:42 25 4
gpt4 key购买 nike

我想从一个对象外部操作一个链表,但它并没有像我想象的那样工作。

情况是这样的:我有一个对象,它具有指向第一个条目的基类指针,以标记链表的开头。

class theObject {
public:
theObject() : mFirstEntry(0), mLastEntry(0) {}
~theObject() {}

template<class T>
void addEntry(const std::string &blah, const std::string &blub, const T &hui)
{
child<T> *newEntry = new child<T>(blah, blub, hui);

if (mFirstEntry) {
mLastEntry->setNext(newEntry);
mLastEntry = newEntry;
}
else {
mFirstEntry = newEntry;
mLastEntry = newEntry;
}
}

base * getFirstEntry() const
{
return mFirstEntry;
}

void printEntrys() const
{
base *data = mFirstEntry;
while(data) {
std::cout << data->getBlah() << data->getBlub() << std::endl;
data = data->getNext();
}
}

private:
base *mFirstEntry;
base *mLastEntry;
};

class base {
public:
base() : mBlah(""), mBlub(""), mNext(0) {}

base(const std::string &blah, const std::string &blub) : mBlah(blah), mBlub(blub), mNext(0) {}

virtual ~base()
{
if (mNext) {
delete mNext;
}
}

void setNext(base *next)
{
mNext = next;
}

base * getNext() const
{
return mNext;
}

std::string getBlah() const
{
return mBlah;
}

std::string getBlub() const
{
return mBlub;
}

protected:
std::string mBlah;
std::string mBlub;
base *mNext;
};

链表的每个条目都是子类型,它是一个模板并继承基类。

template<class T>
class child : public base {
public:
inline child(const std::string &blah, const std::string &blub, const T &hui, base *next = 0) : mHui(hui), base(blah, blub)
{
if(next) {
mNext = next;
}
}

inline child(const child &r)
{
*this = r;
}

inline const child & operator = (const child &r)
{
if (this == &r) return *this;

mBlah = r.mBlah;
mBlub = r.mBlub;
mNext = r.mNext;
mHui = r.mHui;

return *this;
}

inline const T getData() const
{
return mHui;
}

protected:
T mHui;
};

现在我用几个条目填充对象

int main(int argc, char* argv[])
{
theObject data;
int a(0), b(1), c(2), d(3);
const std::string blah("blah"), blub("blub");
data.addEntry(blah, blub, a);
data.addEntry(blah, blub, b);
data.addEntry(blah, blub, c);
data.addEntry(blah, blub, d);

std::cout << "Original entries" << std::endl;
data.printEntrys();

然后我想修改链表

    base *stuff = data.getFirstEntry();
std::cout << "Changed in stuff list" << std::endl;
while(stuff) {
stuff = new child<double>("noBlah", "noBlub", 3.14, stuff->getNext());
std::cout << stuff->getBlah() << stuff->getBlub() << std::endl;
stuff = stuff->getNext();
}

并希望操纵原件......但它接缝我只操纵了一个拷贝

    std::cout << "linked list in data object should now be stuff list" << std::endl;
data.printEntrys();

return 0;
}

有人知道为什么它不起作用吗? getFirstEntry() 返回一个指针,所以我想我会操纵它指向的对象。

最好的问候,本

最佳答案

你通过 base *stuff = data.getFirstEntry(); 得到一个指向第一个元素的指针,然后使用 stuff = new child<double>("noBlah", "noBlub", 3.14, stuff->getNext()); 将它指向一个新对象.换句话说,您更改的是指针,而不是指向的值。

我建议您查看有关指针的教程。曾经有人推荐过这个视频:Binky Pointer Fun

关于c++ - 从对象外部操作链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13365898/

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