4

我正在创建一个包含指向基类的指针的向量。在这个向量中,我动态存储指向包含一些成员变量的派生类的指针,其中一个是字符串变量名。

#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() << ")";
    }
};

我后来在一个主函数中设置了所有这些成员变量。问题是当我尝试输出成员变量的内容时,除了字符串名称之外,它们都显示出来。现在,我已检查以确保在调用 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";
            }
        }
4

1 回答 1

4

您推送到data向量的对象是本地的,因为它们是在 if/else 块中声明的(请参阅fishand的声明animal)。

当您将此类对象的地址推送到向量时,它将继续指向本地对象,该对象在本地范围的末尾不再存在。您需要创建超出本地范围的对象。一种方法是在堆上创建本地对象的副本并将它们推送到向量:

data.push_back(new fishObject(fish));

当然,这意味着您会发生内存泄漏,除非您确保在程序结束前的某个时间明确删除向量的元素。避免考虑这一点的通常建议是使用向量std::unique_ptr<MovingObject>而不是裸指针向量。

于 2012-10-09T03:12:01.473 回答