我是c ++的新手但不是编程,我遇到了一个非常令人困惑的问题。我有一个类在其构造函数中创建一个列表。
正如您所看到的,我以两种不同的方式打印出列表的最终值,这些方式通常彼此一致:一个使用list::end
,另一个使用list::back
。然后我在main函数中将构造函数调用到此类,访问创建的列表,并尝试打印最终值。示例代码如下所示。
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <list>
#include <algorithm>
#include <queue>
using namespace std;
class Process{
public:
Process(int CB);
int CB;
};
Process::Process(int c){
CB = c;
}
class Event{
public:
Event(Process *process);
Process *process;
};
Event::Event(Process *ps){
process = ps;
}
typedef list<Event> EventList;
class DES{
public:
DES(string originFile);
EventList events;
};
DES::DES(string originFile){
ifstream infile (originFile.c_str());
string str;
while (getline(infile, str)) {
// output the line
//cout << str << endl;
istringstream iss(str);
int AT,TC,CB,IO;
if (!(iss >> AT >> TC>>CB>>IO)) {
cout<<"breaking out of while loop \n";
break;
}
Process p(CB);
Event evt(&p);
this->events.push_back(evt);
}
int cb = this->events.back().process->CB;
EventList::iterator inserter2 = this->events.begin();
EventList::iterator inserter3 = this->events.end();
//inserter3--;
//cout<<"CB after while loop using List<>::end(): " <<inserter3->process->CB<<endl;
//cout<<"CB after while loop using LIST<>::back "<<cb<<endl;
infile.close();
}
int main (int argc, char* argv[]) {
string inputFileName = argv[1];
DES des(argv[1]);
EventList::iterator b = des.events.end();
b--;
cout<<"CB at back of list in main: "<<b->process->CB<<endl;
return 0;
}
所以这就是我感到困惑的地方。 main中的print语句应该匹配构造函数中print语句的输出,因为它们都只是打印列表最后一个元素的字段->process->CB
。但是,出于某种原因,这仅在我在构造函数中取消注释行//EventList::iterator inserter2 = this->events.begin();
时才有效。同样,如果我保留该行,而是注释掉EventList::iterator inserter3 = this->events.end();
行,那么它也不起作用。只有当我在BOTH上构造一个迭代器时,列表的结尾和开头才会在main中打印出正确的值。
任何人都可以对这种奇怪的行为有所了解吗?我知道由于我对c ++缺乏熟悉,这一定是一个简单的误解,但我不得不承认这种行为对我来说似乎有点不自然。
编辑:这是输出,其中一个迭代器在构造函数中注释掉:
CB after while loop using List<>::end(): 10
CB after while loop using LIST<>::back 10
CB at back of list in main: 306496
这是构造函数中两个迭代器的输出:
CB after while loop using List<>::end(): 10
CB after while loop using LIST<>::back 10
CB at back of list in main: 10
-Paul
答案 0 :(得分:1)
您在这里使用/存储本地地址:
Process p(AT,TC,CB,IO);
Event evt(AT,&p,CREATED,READY);
一旦声明p
的代码块退出,对该指针的任何引用都会导致未定义的行为。
由于Process
包含一个简单的int
,您只能存储Process
的副本而不是使用指针。
class Process{
public:
Process(int CB);
int CB;
};
class Event{
public:
Event(const Process& process);
Process process;
};
Event::Event(const Process& ps) : process(ps) {}
然后第一个代码块看起来像这样:
Process p(CB);
Event evt(p);
或简单地说:
Event evt(Process(CB));
这应该至少可以消除不一致的结果。
如果您确实需要指针,请考虑智能指针,例如std::unique_ptr
或必要时std::shared_ptr
,而不是使用原始指针。