I have created a simple program that “draws” shapes within command prompt. I am using several class implementation, but the main issue is within the Command abstract class, more specifically the destructor in that class. I am compiling it with a DEBUG mode that I defined that prints a '-' every time the destructor deletes an object.
The Command class looks something like this:
class Command {
public:
Command(){
#ifdef DEBUG
std::cout << '+';
#endif
}
virtual ~Command() {
#ifdef DEBUG
std::cout << '-';
#endif
}
virtual void execute() = 0;
virtual void unexecute() = 0;
};
The loop I am calling within another class is as follows:
vector<Command*> history_;
while(position_ != 0) {
delete *history_.end();
history_.pop_back();
position_--;
}
if position_ is greater than 1 it prints the '-' n – 1 times, but it doesn’t call the destructor on the first delete of the loop.
For an STL container
end()does not references the last item, but the place just after the last item.