【问题标题】:C++ Accessing a reference a derived object in a vector of base object across classesC ++跨类访问基对象向量中的派生对象的引用
【发布时间】:2021-12-19 21:59:43
【问题描述】:

我正在尝试创建一个存储基类的向量,然后将其传递给另一个类,该类然后从基类的向量访问派生类我发现了多个涵盖此问题的 Stack Overflow 问题,但它们都缺少某些方面就像跨类传递它或将多个对象存储在一个向量中。 CPP文件

vector<Item*> Items::cM() {
    vector<Item*> M;
    string line;
    string filePath = "...";
    ifstream stream(filePath);
    while (getline(stream, line)) {
        vector <string> IV= UtilFunctions::splitString(line, ',');
        const char *t= IV[0].c_str();
        switch (*t) {
        case 'a': {
            StarterFood tmpStarter = StarterFood(*a,b,c);//Simplified
            Item* b = &tmpStarter;
            M.push_back(b);
            //If I use b here and do b->toString() works as expected
            break;
        }
        
        }
        
    }
    return M;
}

主要

int main(){
    vector <Item*> i= Items::cM();
    items[0]->toString();//This is an overloaded function that all of the derived classes 
    //Throws the error Access violation reading location 0xCCCCCCCC.
    have such as starterfood
    system("pause");
    return 0;
}

如果需要更多信息,请随时询问。谢谢我也尝试过传递一个指针然后取消引用该指针,但我认为切片我的对象只留下基类,我尝试实现 unique_ptr 但我收到一个语法错误,说没有从 starterFood 返回 unique_ptr 的重载。 错误是访问冲突读取位置 0xCCCCCCCC。

【问题讨论】:

标签: c++ oop pointers derived-class base-class


【解决方案1】:

按照建议,我使用了 unique_ptr,我必须在派生类上使用 make_unique,并在存储在向量中时移入派生类,并将所有 vector 替换为 vector 但它现在可以工作了。

vector<unique_ptr<Item>> Items::cM() {
    vector<unique_ptr<Item>> M;
    string line;
    string filePath = "...";
    ifstream stream(filePath);
    while (getline(stream, line)) {
        vector <string> IV= UtilFunctions::splitString(line, ',');
        const char *t= IV[0].c_str();
        switch (*t) {
        case 'a': {
            unique_ptr<StarterFood> tmpStarter = make_unique<StarterFood> 
            (*a,b,c);
            M.push_back(std::move(tmpStarter));
            break;
        }
        
        }
        
    }
    return M;
}

然后在main中

    vector<unique_ptr<Item>> i= Items::cM();
    items[0]->toString();//This is an overloaded function that all of the 
    derived classes
    system("pause");
    return 0;

这解决了我最初尝试的对象切片问题。

【讨论】:

    猜你喜欢
    • 2013-02-03
    • 2015-05-23
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多