C ++ string member variable missing in vector

I am creating a vector containing pointers to a base class. In this vector, I dynamically store pointers to derived classes that contain some member variables, one of which is the name of a string variable.

#include "stdafx.h" #include <iostream> #include <vector> #include <string> #include <cstdlib> bool hasDirection = false; bool hasDiameter = false; int direction; float diameter; int starDimension = 0; int animalDimension = 0; int fishDimension = 0; class MovingObject { protected: std::string name; int direction; float diameter; int dimension; float movingSpeed; public: std::string getName(){ return name;}; int getDirection(){ return direction;}; float getDiameter(){ return diameter;}; float getMovingSpeed(){ return movingSpeed;}; int getDimension(){ return dimension;}; void setName(std::string v){ name = v;}; void setDirection(int d){ direction = d;}; void setDiameter(float f){ diameter = f;}; void setMovingSpeed(float s){ movingSpeed = s;}; void setDimension (int d){ dimension = d;}; virtual void PrintContents()=0; }; static std::vector<MovingObject*> data; class starObject : public MovingObject { public: void PrintContents() { std::cout << "(" << getName() << "," << getDiameter() << "," << getDirection() << ")"; } }; class animalObject : public MovingObject { public: void PrintContents() { std::cout << "(" << getName() << "," << getDiameter() << "," << getDirection() << ")"; } }; class fishObject : public MovingObject { public: void PrintContents() { std::cout << "(" << getName() << "," << getDiameter() << "," << getDirection() << ", [" << getDimension() << "], " << getMovingSpeed() << ")"; } }; 

I later set all these member variables inside the main function. The problem is that when I try to display the contents of member variables, all of them are displayed except for the string name. Now I checked that the string was set before calling the PrintContent () method, and it shows that the value is in the vector. However, when I debug the code, the value no longer exists, instead it contains an empty string.

Can someone with better C ++ knowledge explain to me why this is happening? This is the main class:

 int main() { std::string type; Reader reader; while (!std::cin.eof()) { try { std::string type; std::cin >> type; if (type =="int") { reader.ReadDirection(); } else if (type =="float") { reader.ReadDiameter(); } else if (type == "string") { std::string name; std::cin >> name; if (hasDirection && hasDiameter) { int dimension; if (diameter > 0 && diameter < 10) { //fish fishObject fish; fish.setName(name); fish.setDiameter(diameter); fish.setDirection(direction); dimension = fishDimension; fishDimension += 50; fish.setDimension(dimension); fish.setMovingSpeed(0.1); data.push_back(&fish); } else if (diameter >= 10 < 500) { //animal animalObject animal; animal.setName(name); animal.setDiameter(diameter); animal.setDirection(direction); dimension = animalDimension; animalDimension += 800; animal.setDimension(dimension); animal.setMovingSpeed(5.0); data.push_back(&animal); } else if (diameter >=500) { //star starObject star; star.setName(name); star.setDiameter(diameter); star.setDirection(direction); dimension = starDimension; starDimension += 5000; star.setDimension(dimension); star.setMovingSpeed(30.0); data.push_back(&star); } } else { throw (IncompleteData(name)); } } } catch (IncompleteData e) { std::cerr << "No diameter or direction given for object " << e.objectName << "\n"; } } 
+4
source share
1 answer

The objects that you click on the data vector are local because they are declared inside if / else blocks (see fish and animal declarations).

When you click the address of such an object on a vector, it will continue to point to a local object that ceases to exist at the end of the local area. You need to create objects that live outside the local area. One way to do this is to create copies of local objects on the heap and click them on the vector:

 data.push_back(new fishObject(fish)); 

Of course, this means that you get a memory leak if you are not sure that you explicitly deleted the vector elements some time before the end of the program. The usual recommendation, not to think about it, is to use the std::unique_ptr<MovingObject> vector instead of the bare pointer vector.

+4
source

Source: https://habr.com/ru/post/1438537/


All Articles