You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

删除原对象后无法访问vector中存储的类对象属性的问题咨询

门店服装报价应用内存管理问题

问题核心

我正在开发一款门店服装报价应用,在Store类中定义了std::vector<Clothes*>来存储Shirt、Pants等派生类服装对象的指针。现在遇到的问题是:删除原对象指针指向的内存后,无法访问vector中存储的对象属性。我原本以为std::vector会调用拷贝构造函数并拥有独立的存储空间,但不清楚为何无法访问修改vector内的对象,尝试不同方案时编译器会报不同的读写访问错误。

补充说明

若不删除存入vector后的原对象指针,代码可正常运行,还能从vector中移除该对象并添加新对象,且新对象存入vector后可删除其原指针。

实际需求

创建Shirt类的局部对象,将其存入vector,当创建它的函数或构造函数执行完毕后局部对象销毁,但仍能访问并修改vector中存储的该对象数据。

相关代码

Store.h

#pragma once
#ifndef STORE_H
#define STORE_H
#include <string>
#include <vector>

class Clothes;
class Shirt;
class Pants;

class Store
{
private:
    std::string storeName;
    std::string address;
    std::vector<Clothes*> clothesList;

    Shirt* _shirt;
    Pants* _pants;

public:
    Store();
    ~Store();
    void setClothesPreferences(int clothesType, int alternative1, int alternative2, int quality);
    
};

#endif // !STORE_H

Store.cpp

#include "Store.h"
#include "Clothes.h"
#include "Shirt.h"
#include "Pants.h"
#include <iostream>

Store::Store()
{
    std::cout << "Please, introduce your store name: ";
    std::getline(std::cin, storeName);
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

    std::cout << "Store address: ";
    std::getline(std::cin, address);
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

    _shirt = new Shirt();
    _pants = new Pants();

    clothesList.push_back(_shirt);
    clothesList.push_back(_pants);

// If I use delete _shirt here then I would get an error in setClothesPreferences()
}

Store::~Store()
{
    std::cout << "Store closed" << std::endl;
}

void Store::setClothesPreferences(int clothesType, int alternative1, int alternative2, int quality) 
{
    if (clothesType == 1)
    {
        Shirt* _shirt2 = dynamic_cast<Shirt*>(clothesList.front()); // Type-casting from Clothes (base class) to Shirt again (derived class)
        clothesList.erase(clothesList.begin());

        // SHIRT SPECIFICATIONS FOR FURTHER QUOTE
        if (alternative1 == 1)
            _shirt2->shortSleeve = true;
        else
            _shirt2->shortSleeve = false; // longSleeve

        if (alternative2 == 1)
            _shirt2->mandarinCollar = true;
        else
            _shirt2->mandarinCollar = false; // regularCollar

        if (quality == 1)
            _shirt2->quality; // Standard 
        else
            _shirt2->quality; // Premium

        clothesList.insert(clothesList.begin(), _shirt2);
    }
}

Clothes.h

#pragma once
#ifndef CLOTHES_H
#define CLOTHES_H
#include <string>

class Clothes
{
public:
    std::string derivedName;
    std::string quality[2];
    double unitPrice;
    int stockAmount;

    Clothes();
    virtual ~Clothes();
};
#endif // !CLOTHES_H

Clothes.cpp

#include "Clothes.h"

Clothes::Clothes()
{
    derivedName = "Base default";

    quality[0] = "Standard";
    quality[1] = "Premium";

    unitPrice = NULL;
}

Clothes::~Clothes()
{
}

Shirt.h

#pragma once
#ifndef SHIRT_H
#define SHIRT_H
#include "Clothes.h"

class Shirt : public Clothes
{
public:
    bool shortSleeve;
    bool mandarinCollar;

    Shirt();
    ~Shirt() override;
};

#endif // !SHIRT_H

Shirt.cpp

#include "Shirt.h"

Shirt::Shirt()
{
    derivedName = "Shirt";
    stockAmount = 2000;
}

Shirt::~Shirt()
{
}

内容的提问来源于stack exchange,提问作者Pedro Rob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 08:11:27