C++ vector 中不存在的字符串成员变量
C++ string member variable not present in vector
我正在创建一个包含指向基类的指针的向量。在这个向量中,我动态存储指向派生类的指针,这些派生类包含一些成员变量,其中一个是字符串变量名称。
#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() << ")";
}
};
后来我在 main 函数中设置了所有这些成员变量。问题是当我尝试输出成员变量的内容时,除了字符串名称外,所有成员变量都显示出来。现在,我已经检查以确保在调用 PrintContent() 方法之前设置了字符串,它显示该值在向量中。但是,当我通过代码进行调试时,该值不再存在,而是包含一个空字符串。
具有更好c ++知识的人可以向我解释为什么会发生这种情况吗?这是主类:
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";
}
}
你推送到data
向量的对象是局部的,因为它们是在 if/else 块中声明的(参见 fish
和 animal
的声明)。
当您将此类对象的地址推送到向量时,它将继续指向本地对象,该对象在本地范围的末尾不复存在。您需要创建超出本地范围的对象。一种方法是在堆上创建本地对象的副本,并将其推送到向量:
data.push_back(new fishObject(fish));
当然,这意味着你会得到内存泄漏,除非你确保在程序结束前的某个时间显式删除向量的元素。避免考虑这一点的通常建议是使用 std::unique_ptr<MovingObject>
向量而不是裸指针向量。
相关文章:
- 使用 const char* 初始化 const ref 字符串成员时幕后会发生什么
- 具有字符串成员的结构的动态数组
- 为什么在类体中无法启动静态常量字符串成员
- 引用字符串成员变量
- 不能将类模板中使用的对象的字符串成员变量分配给C++
- 在C#中,是否有与INT和字符串成员变量相当的C 联合
- 清理所拥有的(!)字符串成员时,析构函数偶尔崩溃
- C :方法中的字符串成员别名
- 在 C++ 中初始化静态字符串成员
- C++如何访问结构列表的字符串成员
- 如何引用结构字符串成员
- const字符串成员的错误:没有适当的默认构造函数可用
- C 字符串成员函数subtr使用
- 使用字符串成员函数格式化SSN
- C++ vector 中不存在的字符串成员变量
- 如何在 c++ 中的类中声明字符串成员
- 使用函数调用初始化字符串成员c++
- C++ 不能将公共 const 字符串成员作为参数传递给同一类的成员函数
- 类中包含的 STL 字符串成员包含在 std::vector 中
- 如何从 char 数组中提取数字字符串成员