4

基本クラスへのポインターを含むベクターを作成しています。このベクトルでは、いくつかのメンバー変数を含む派生クラスへのポインターを動的に格納しています。そのうちの 1 つは文字列変数名です。

#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

ベクトルにプッシュするオブジェクトは、 if / elseブロック内で宣言されているためローカルです(およびdataの宣言を参照)。fishanimal

このようなオブジェクトのアドレスをベクターにプッシュすると、ローカルオブジェクトをポイントし続け、ローカルスコープの最後に存在しなくなります。ローカルスコープを超えて存在するオブジェクトを作成する必要があります。これを行う1つの方法は、ヒープ上にローカルオブジェクトのコピーを作成し、それらをベクターにプッシュすることです。

data.push_back(new fishObject(fish));

もちろん、これは、プログラムの終了前にベクターの要素を明示的に削除しない限り、メモリリークが発生することを意味します。これを考える必要を避けるための通常の推奨事項はstd::unique_ptr<MovingObject>、裸のポインタのベクトルの代わりにのベクトルを使用することです。

于 2012-10-09T03:12:01.473 に答える