I'm having some trouble working out how to write a nicely polymorphic annealer class. I'm sure I'm doing something very wrong, and that this is a duplicate question but I have been searching for a long time and not found anything.
I want to take a pointer to an instance of the abstract type State (called pState) and use the pure virtual functions Perturb() (Which randomly changes the state a bit) and Cost() (Which evaluates the cost of the function) to find a state that minimises the cost function.
I need to keep track of multiple subclass instances in this function, and I believe that either the declaration or assignment to these variables is causing an issue.
void Annealer::Minimise(State *pState){
//Set up
....
....
State *state;
State *newState;
*state = *pState;
*newState = *state;
//Evaluate the initial cost
pState->Cost(); //Works just fine
double cost = state->Cost(); //Segfault
....
}
Calling Cost on the subclass pointer before the attempt to copy is successful. I have not included the subclass because it is very long and complex.
Edit:
State is defined as follows:
class State {
public:
virtual void Perturb()=0;
virtual double Cost()=0;
};