c++ 我如何添加(或追加)类对象到不同的类对象数组

polhcujo  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(128)

我有这个Book类对象(基类:(x月1日至1x日)

Book books[5] = {
    Book("Kucuk Prens","FR","Roman","Book",10,15,103061,12,"Kultur yayinlari",10),
    Book("Kucuk Prens1","FR","Roman","Book",10,15,103061,12,"Kultur yayinlari",10),
    Book("Kucuk Prens2","FR","Roman","Book",10,15,103061,12,"Kultur yayinlari",10),
    Book("Kucuk Prens3","FR","Roman","Book",10,15,103061,12,"Kultur yayinlari",10),
    Book("Kucuk Prens4","FR","Roman","Book",10,15,103061,12,"Kultur yayinlari",10)};

我有一个Cart类。

class Cart
{

private:

    int totalPrice;
    Product productList[5]; // kullanıcın seçtiği

public:

};

例如,我想把books[1]加到productList[]上,怎么做呢?

fzsnzjdm

fzsnzjdm1#

首先,您正在编写C++,这意味着您足够幸运,没有****使用C数组/malloc 'd指针,而是更喜欢STL容器,因为它们为您管理内存!
也就是说,如果你想把派生类放到一个基类容器中,你就必须存储指向那个基类的指针,再一次,最好使用现代C++,使用智能指针,因为它们是自我管理的,可以让你省去free 'ing的麻烦:

#include <memory> //smart pointers
#include <vector> //vector, STL container for a dynamically resizable array
#include <string> //string, pretty self explanatory
#include <algorithm> //algorithm, for powerful utility algorithms

class Cart
{

private:

    int totalPrice;
// A vector of smart pointers to our base class
    std::vector<std::unique_ptr<Product>> productList;

public:

    Cart() = default;

    void addProduct(std::unique_ptr<Product>&& product)
    {
// we put our product in the cart using [Move Semantics][1], transferring ownership 
// of the pointer to the cart.
        productList.push_back(std::move(product));
    }

    void removeProduct(std::string_view product_name)
    {
//we use a combination of std::find_if, from the algorithm header, together with a 
//lambda, to remove a product by its name (similar functions can be written using
//different attributes as keys
        auto const& to_remove = std::find_if(productList.begin(), productList.end(), 
        [
            &product_name
        ](auto const& product)
        {
            return product->getName() == product_name;
        }
        );

//if we have found our product, we remove it...
        if (to_remove != productList.end())
        {
            productList.erase(to_remove);
        }
    }

    // return a const reference to the productList, this way we can iterate 
    // over the lists from outside the class without modifying it.
    std::vector<std::unique_ptr<Product>> const& getProductList() const
    {
        return productList;
    }

    // add other methods, like getTotalPrice() etc...
};

一开始,这个解决方案可能看起来有点难以接受,因为它与C语言有很大的不同,但是一旦你熟悉了STL和这些更高级的结构(比如lambdas),你就会更安全、更快地编写代码。
从这里开始,这个类的用法示例可以是:

#include <iostream>

int main()  
{
    Cart cart;

    cart.addProduct(std::make_unique<Product>(10, "Apple"));
    cart.addProduct(std::make_unique<Product>(20, "Banana"));
    cart.addProduct(std::make_unique<Product>(30, "Orange"));

    cart.removeProduct("Banana");
//modern range based for loop on the returned const reference
    for(auto const& product : cart.getProductList())
    {
        std::cout << product->getName() << " " << product->getPrice() << std::endl;
    }
    return 0;
}

相关问题