In the constructor of a class I have I create an array with the needed size like this:
ArrayClass::ArrayClass(int size)
{
Number* nmbr = new Number[size];
}
and
ArrayClass::ArrayClass()
{
Number* nmbr = new Number[2];
}
I also have it specified in the header as
Number* nmbr;
While the creation of the array itself works I can’t seem to access it outside of the constructor. It seems like whenever I leave the constructor the variable gets freed from the memory. How do I prevent this so I can use the variable when calling the other functions in the class?
Try to declare the variable nmbr in the declaration of the class and not in the constructor.
Example: