簡體   English   中英

包含shared_ptr的std :: list的C ++重載[]

[英]C++ Overloading [] for std::list containing shared_ptr

我正在嘗試為包含shared_ptrlist重載[]運算符(類似於std::vector::operator[] )。 它需要在位置index處返回對元素的引用(我已經給出了設計規格)。

cartruck類別來源於抽象的基本類別的vehicle dealership包含一個std::list<std::shared_ptr<vehicle>> dealershipLot;

這就是我試圖重載[]運算符的方式; std::list<std::shared_ptr<vehicle>>& Dealership::operator[](size_t index)

我嘗試使用std::find來獲取元素位置的迭代器,並使用&(findIter)返回引用&(findIter)但似乎std::find需要重載==才能與列表類型一起工作,但出現錯誤

binary ==: no operator found which takes a left-hand operand of type std::shared_ptr<vehicle> (or there is no acceptable conversion)

以下是我的代碼的簡化版本:

#include <vector>
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <memory>
#include <fstream>
using namespace std;

class vehicle {
protected:
    string name;
public:
    vehicle(){ name.clear(); }
    vehicle(string v) : name(v){};
    string getName() const { return name; };
    virtual void display(ostream&) const = 0;
};

class Car : public vehicle {
protected:
    int no;
public:
    Car(){ no = 0; };
    Car(string n, int no) : vehicle(n), no(no) {};
    std::string getName() const { return name; }
    int getNo() const{ return no; }
    void display(ostream& os) const {
        os << name << " " << no << std::endl;
    }
};

class Truck : public vehicle {
protected:
    int no;
    int km;
public:
    Truck(){ no = 0; };
    Truck(string n, int no, int km) : vehicle(n), no(no), km(km) {};
    std::string getName() const { return name; }
    int getNo() const{ return no; }
    int getKm() const{ return km; }
    void display(ostream& os) const {
        os << name << " " << no << "" << km << std::endl;
    }
};

class Dealership{
    string dealershipName;
    std::list<std::shared_ptr<vehicle>> dealershipLot;
public:
    Dealership(){ dealershipName.clear(); dealershipLot.clear(); };
    Dealership(const std::string n);
    Dealership(const Dealership&); //Copy constructor
    Dealership& operator=(const Dealership&); //Copy assignment operator
    Dealership(Dealership&&); //Move constructor
    Dealership&& operator=(Dealership&&); //Move assignment operator
    void operator+=(std::shared_ptr<vehicle> veh); //Operator += overload 

    bool operator==(const std::shared_ptr<vehicle> other){  //???
        return dealershipName == other->getName();
    }

    std::list<std::shared_ptr<vehicle>>& operator[](size_t index){ //???

        /*size_t index = 3;
        std::list<std::shared_ptr<vehicle>>::iterator findIter =
            std::find(dealershipLot.begin(), dealershipLot.end(), index);
        cout << &(findIter) << endl;
        return &(findIter);*/
    }
};

int main()
{
    Dealership d1("lot3");
    d1 += std::move(std::shared_ptr<vehicle>(new Car("Toyota", 15)));
    return 0;
}

如何重載[]運算符以獲取對list元素的引用?

編輯:對於任何感興趣的人,我都將[]重載為;

//Operator [] return reference to element at position n
    vehicle& Task::operator[](size_t index){
        std::list<std::shared_ptr<vehicle>>::iterator it = dealershipLot.begin();
        std::advance(it, index);
        return **it;
    }

我創建了一個迭代器,該迭代器可通過dealershipLot列表index位置前進,然后返回該迭代器指向的對象作為參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM