简体   繁体   English

C ++从函数返回对象?

[英]c++ returning object from function?

Absolute newbie to c++ (and oop) as well. 绝对是C ++(和oop)的新手。

Just wanted to ask how to return an object from a list (if it exists), by passing a single id to a getter. 只是想问如何通过将单个id传递给getter来从列表(如果存在)中返回对象。

My code is as follows: 我的代码如下:

    class Customer
    {
    private:
        unsigned int custNo;
        std::string  name;
        std::string  address;
    /* .. */ 
    }

    class Store
    {
    private:
        std::string storeName;
        std::list<Customer *> customerBase;
        std::list<Product *>  productStock;
        std::list<Sale*>      sales;
    public:
        Store(std::string storeName); // constructor
        std::string getStoreName();
        Customer & getCustomer(unsigned int custId);   //METHOD IN QUESTION

    /*..*/

    // Constructor

    Customer::Customer(std::string name, std::string address)
    {
        //ctor
    }


    //

    Customer & Store::getCustomer(unsigned int custId){


    }   

I know this might be a farily basic question. 我知道这可能是一个基本问题。 Still I would very much appreciate the help. 我仍然非常感谢您的帮助。 Thanks in advance! 提前致谢!

Just wanted to ask how to return an object from a list (if it exists), by passing a single id to a getter. 只是想问如何通过将单个id传递给getter来从列表(如果存在)中返回对象。

Pointer is the first thing that you should think when you see "if it exists". 指针是您看到“如果存在”时应该考虑的第一件事。 This is because the only representation of an object in C++ that can be optional is a pointer. 这是因为在C ++中,对象的唯一表示形式是可选的。 Values and references must always be present. 值和引用必须始终存在。 Therefore, the return type of your function should be Customer* , not Customer& : 因此,函数的返回类型应该是Customer* ,而不是Customer&

Customer* Store::getCustomer(unsigned int custId){
    ...
}

If you need fast retrieval by an id , use a map<int,Customer*> or unordered_map<int,Customer*> . 如果需要通过id快速检索,请使用map<int,Customer*>unordered_map<int,Customer*> You could do it with a list, too, but the search would be linear (ie you would go through the entire list in the worst case). 您也可以使用列表来执行此操作,但是搜索将是线性的(即,在最坏的情况下,您将遍历整个列表)。

Speaking of pointers, if you must store pointers to Customer objects, assuming that the objects themselves are stored in some other container, you may be better off using shared_ptr<Customer> in both containers, to simplify resource management. 说到指针,如果必须存储指向Customer对象的指针(假设对象本身存储在其他容器中),则最好在两个容器中都使用shared_ptr<Customer>来简化资源管理。

You can do this but it would be cumbersome as list is not sorted so you have to traverse the list and check each structure for matching id. 您可以执行此操作,但是这样做很麻烦,因为列表未排序,因此您必须遍历列表并检查每个结构的ID是否匹配。

Rather you could store these in std::map with ids as their keys...OR much better unordered_map if you really care about performance. 相反,您可以将这些存储在以id为键的std :: map中……或者,如果您真的在乎性能的话,最好使用unordered_map。

Assuming you have getCustId() public member function in class Customer : 假设您在类Customer具有getCustId()公共成员函数:

Customer & Store::getCustomer(unsigned int custId){

    auto custIt = find_if(customerBase.begin(), customerBase.end(), 
        [custId](const Customer& c){ return c.getCustId() == custId });

    return *custIt;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM