我想操纵一个来自对象外部的链接列表,但它不像我那样工作。
情况如下: 我有一个对象,它有一个基类的指针到第一个条目,以标记链表的开头。
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;
}
anybode知道为什么它不起作用吗? getFirstEntry()返回一个指针,所以我想我会操纵它指向的对象。
祝你好运, 本
答案 0 :(得分:0)
通过base *stuff = data.getFirstEntry();
获得指向第一个元素的指针,然后使用stuff = new child<double>("noBlah", "noBlub", 3.14, stuff->getNext());
将其指向新对象。换句话说,您更改指针,而不是指向的值。
我建议你查看关于指针的教程。有人曾推荐过这段视频:Binky Pointer Fun
答案 1 :(得分:0)
返回指针并更改 local 指针,而不更改指针指向的值! 你应该这样做:
*stuff = child<double>("noBlah", "noBlub", 3.14, stuff->getNext());
通过这种方式,您实际上可以更改指针所指向的对象。
LG ntor
答案 2 :(得分:0)
好的,感谢ntor我能够解决这个问题。
这是代码示例(从头开始的代码仍然有效)
template<class T>
child<T> * getnewEntry(const std::string &a, const std::string &b, const T &c, base *next)
{
child<T> *nc= new child<T>(a, b, c, next);
return nc;
}
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);
base *stuff = data.getFirstEntry();
stuff = getnewEntry("noBlah", "noBlub", 3.14, stuff->getNext());
data.mFirstEntry->setNext(0);
delete data.mFirstEntry;
data.mFirstEntry = stuff;
while (stuff->getNext()) {
base *tmpstuff = getnewEntry("noBlah", "noBlub", 3.14, stuff->getNext()->getNext());
stuff->getNext()->setNext(0);
delete stuff->getNext();
stuff->setNext(tmpstuff);
stuff = tmpstuff;
}
data.mLastEntry = stuff;
return 0;
}
现在更改链接列表位置的对象并更新链接列表