Right now I'm working on a program that uses a class that contains pointers to other classes. The problem I am having is trying to figure out where to instantiate the classes using the "new" command so that they remain in memory after function that allocates/creates them. It seems to me that this should be done in the class constructor, although when I try this, the class is correctly created then deleted once the constructor exits leaving the pointer to the class to be undefined. This is how I understand the "new" command anyway. Am I correct in this statement or do I have it confused with something else, and how can I instantiate these objects so that they are not deleted and the objects that point to them left undefined at the end of the fuction?
I have it coded as below currently:
class ConstrLst: public xorlist
{
public:
ConstrLst()
{
Compfunct=cmpConstrPtrs;
}
};
class connector
{
protected:
char id[6]; float value;
ConstrPtr informant;
ConstrLst *constrsP; //pointer to the above class
public:
connector(char *name);
int HasValue()
{
return informant!=0;
}
void ShowName()
{
printf("%s", id);
}
float GetValue()
{
return value;
}
void SetValue(float NewValue, ConstrPtr Setter);
void ForgetValue(ConstrPtr retractor);
void connect(ConstrPtr NewConstraint);
void ForEachExcept(ConstrPtr, message);
};
// and the constructor is:
connector::connector(char *name)
{
strcpy(id, name);
informant=0;
constrsP=new ConstrLst;
printf("Connector constructor\n");
}