I'm currently working with pointers and I'm wondering if I'm doing something that simply does not work.
For some background, I have created a base class and a derived class. This derived class inherits some member variables and some get/set functions for these variables. This derived class also implements two pure virtual functions that it inherits from the base class.
Now in the main file that contains the code that interacts with these classes, I have three functions, call them f1, main, and f2.
F1 creates an instance of the derived class and returns a base class pointer pointing to this derived class. Within this class F1, I checked to see the int member variable has been set to the correct number, and I also check to see if the virtual functions that the derived class inherits was correctly implemented. Both the variable and the function are set/working properly in the F1 Scope.
Main recieves this base class pointer pointing to this derived class. Within this main function, I again check to see the int member variable has been set to the correct number, and I also check to see if the virtual functions that the derived class inherits was correctly implemented. Both the variable and the function are set/working properly in the Main Scope.
However, when I pass the pointer as an base class pointer argument to function F2. The int member variable gets set to 0, and when I access the virtual function, I recieve a segmentation fault/core dump error.
In a effort to further narrow down the causes, I accessed the derived class implementation files, and I created an instance of the object within the implementation files, and saw that everything was working as expected.
To sum up, I have a base class pointer pointing to a derived class object. Function calls work perfectly on this pointer in the function that it is made and in the function that it is returned to. It is only when the pointer is passed on as an argument of the base class pointer that I begin to see member variables getting set to 0 and the function calls leading to a segmentation fault/coredump.
Is what I'm trying to do something not allowed in C++? Thanks for any help anyone can give me.
edited: I've attached code below. Creature is the base case; Goblin is the derived Case; function F1:
Creature* characterSelect()
{
Creature* creature;
Goblin gb1 = Goblin();
creature = &gb1;
return creature;
}