I have a class, Agent, with a member attribute pointer to an object of another class, Form:
Agent.h
...//snip includes
class Agent{
private:
Form* mvForm_ptr;
...
public:
Agent();
~Agent();
...//snip additional functionality, but no copy-constructor
};
Agent.cpp
#include "Agent.h"
Agent::Agent(){
mvForm_ptr = new Form();
}
Agent::~Agent(){
delete mvForm_ptr;
}
...
As you can see, I do not have an explicit copy-constructor for Agent. Later, I use Agent as follows:
Agent player;
std::vector<Agent> agentsVector;
agentsVector.push_back(player);
This seems to be the cause of a SIGSEGV crash whose error report claims that ~Agent is throwing an EXC_BAD_ACCESS exception. Reading up on vector::push_back here it seems that push_back attempts to copy the passed in value. Since I don't have a copy-constructor in class Agent, what happens to the Form pointer upon an implicit copy attempt? If the pointed-to value is lost in the compiler-generated implicit copy-constructor, would adding an explicit copy-constructor resolve the bad access exception? How should a copy-constructor for class Agent above be implemented? Is this an example of the premise described by the Rule of Three?