c++ - 如何归还或护理项目 list 基于几个类别

标签 c++ class pointers

在我的函数“func”中,我想创建一个对象菜单,该菜单返回含柠檬和咖啡的早餐。 'func'返回菜单列表
当我尝试显示仅包含柠檬或仅咖啡的早餐菜单时,它会正确显示。例如:

c.push_back(make_unique<Breakfast>("eggs", 10)); 
但是当我尝试显示还包含柠檬和咖啡的菜单时,它向我显示了主菜单中的错误
这是程序:
#include <iostream>
#include <vector>
using namespace std;

class Menu {
private:
    int price;
public:
    Menu(int p = 0) : price{ p } {}
    virtual string description() = 0;
    virtual int getPrice() {
        return price;
    }
    virtual ~Menu() {}
};
class WithLemon : public Menu {
private:
    Menu* meniu;

public:
    
    WithLemon(Menu* n) :
        meniu{ n } {}
    string description() override {
        return meniu->description() + " with lemon ";
    }
    int getPrice() override {
        return meniu->getPrice() + 4;
    }
};

class WithCoffee : public Menu {
private:
    Menu* meniu;
public:
    WithCoffee(Menu* n) :
        meniu{ n } {
    }
    string description() override {
        return meniu->description() + " with coffee ";
    }
    int getPrice() override {
        return meniu->getPrice() + 5;
    }
};

class Breakfast : public Menu {
private:
    string name;
public:
    Breakfast(string n, int p) :
        name{ n }, Menu{ p }  {
    }
    string description() override {
        return name;
    }
};
std::vector<std::unique_ptr<Menu>>  func(void)
{
    std::vector <std::unique_ptr<Menu> > c;
    
    
    Breakfast a{ "breakfast eggs", 10 };
    WithCoffee breakfast_with_coffee{ &a };
    Menu* breakfast_with_coffee_and_lemon = new WithLemon{ &breakfast_with_coffee };
    //cout << breakfast_with_coffee_and_lemon->description() << " " << breakfast_with_coffee_and_lemon->getPrice();// print ----> breakfast eggs with coffee  with lemon  19
    
    c.push_back(make_unique<WithLemon>(&breakfast_with_coffee));
    return c;
}
int main() {

    std::vector < std::unique_ptr<Menu> > lista = func();
    for (int i = 0; i < lista.size(); i++) {
        cout << lista[i]->description() << " " << lista[i]->getPrice() << endl; //error read memory access
    }
    return 0;
}

最佳答案

您不能将指针指向自动存储,将其存储在智能指针中并离开该功能。离开该功能后,将释放自动内存,并且智能指针包含一个悬空指针。避免此问题和其他内存泄漏问题的最简单方法是对所有变量使用智能指针:

#include <iostream>
#include <memory>
#include <string>
#include <vector>

class Menu {
private:
    int price;
public:
    Menu(int p = 0) : price{ p } {}
    virtual std::string description() = 0;
    virtual int getPrice() {
        return price;
    }
    virtual ~Menu() = default;
};

class WithLemon : public Menu {
private:
    std::unique_ptr<Menu> meniu;

public:
    WithLemon(Menu* n) : meniu{ n } {}
    std::string description() override {
        return meniu->description() + " with lemon ";
    }
    int getPrice() override {
        return meniu->getPrice() + 4;
    }
};

class WithCoffee : public Menu {
private:
    std::unique_ptr<Menu> meniu;
public:
    WithCoffee(Menu* n) :
        meniu{ n } {
    }
    std::string description() override {
        return meniu->description() + " with coffee ";
    }
    int getPrice() override {
        return meniu->getPrice() + 5;
    }
};

class Breakfast : public Menu {
private:
    std::string name;
public:
    Breakfast(std::string n, int p) : Menu{ p }, name{ n }  {}
    std::string description() override {
        return name;
    }
};

std::vector<std::unique_ptr<Menu>>  func(void) {
    std::vector <std::unique_ptr<Menu> > c;
    
    auto a = std::make_unique<Breakfast>("breakfast eggs", 10);
    auto breakfast_with_coffee = std::make_unique<WithCoffee>(a.release());
    //Menu* breakfast_with_coffee_and_lemon = new WithLemon{ breakfast_with_coffee };
    //std::cout << breakfast_with_coffee_and_lemon->description() << " " << breakfast_with_coffee_and_lemon->getPrice();// print ----> breakfast eggs with coffee  with lemon  19
    
    c.push_back(std::make_unique<WithLemon>(breakfast_with_coffee.release()));
    return c;
}

int main() {

    std::vector < std::unique_ptr<Menu> > lista = func();
    for (const auto &i : lista) {
        std::cout << i->description() << " " << i->getPrice() << std::endl; //error read memory access
    }
    return 0;
}
避免使用原始的newdelete。避免指向自动内存的指针。

关于c++ - 如何归还或护理项目 list 基于几个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62627899/

相关文章:

java - 为什么这个 java 代码没有在 Eclipse IDE 上显示。公共(public)类型App必须在自己的文件中定义

c# - 有没有一种可移植的方法可以在 C# 中复制一 block 内存?

c - gcc 说“来自不兼容指针类型的赋值 [默认启用]

c++ - "assimp/config.h"未找到,但它在文件夹中

c++ - 将以毫秒为单位的 Unix 时间戳转换为正常日期

c++ - 如何在 C/C++ 中将字符串从 UTF8 转换为 Latin1?

css - 如何像我们在 Javascript 中那样(真正地)将参数传递给 CSS 类?

c++ - 创建对取消引用指针的引用?

java - 为什么我们可以在类加载之前使用new运算符

c++ - 在构造函数中初始化引用