C++ 如何在for循环中创建多个具有不同名称的对象?

C++ 如何在for循环中创建多个具有不同名称的对象?,c++,object,C++,Object,我有以下代码: // Generate objects from type DepositoFresco or DepositoNormal number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo; int j; for (j = 0; j < number_depositosf; ++j) { type_deposito = 0; id_deposito = "df" +

我有以下代码:

// Generate objects from type DepositoFresco or DepositoNormal

number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;

int j;
for (j = 0; j < number_depositosf; ++j) {

    type_deposito = 0;
    id_deposito = "df" + to_string(j);
    number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;

    list_depositos.push_back(new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance));
}
//从类型DepositorResco或DepositonNormal生成对象
数量=兰德()%(valormaximo-valorminimo+1)+valorminimo;
int j;
对于(j=0;j
这段代码可以工作,但我想要的是创建具有不同名称的对象(具体来说,名称在“id_deposito”变量中)。我试着做一些事情,比如:

number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;

int j;
for (j = 0; j < number_depositosf; ++j) {

    type_deposito = 0;
    id_deposito = "df" + to_string(j);
    number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;

    DepositoFresco id_deposito = new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance)
    list_depositos.push_back(id_deposito);
}
number\u depositosf=rand()%(valormaximo-valorminimo+1)+valorminimo;
int j;
对于(j=0;j
但它不起作用。
有人知道如何修复它吗?

< P>没有办法在C++中从字符串中创建或修改变量名,但是有一些解决办法。 在这种情况下工作得更好的可能是哈希表。哈希表是一种在对象之间创建单向关联的数据结构,因此,如果您有一个对象O1,则可以轻松检索先前保存的另一个O2。 在本例中,您希望使用字符串访问DepositorResco对象

首先,你需要包括:

#include <map>
希望有帮助!:包括
#include <random>
#include <string>
#include <iostream>
#include <sstream>

class Deposito
{
private:
    int type_deposito;
public:
    Deposito(int type_deposito):
        type_deposito(type_deposito)
    {}

    void testFunc()
    {
        std::cout << "You called a deposito testFunc with type: " << this->type_deposito << std::endl;
    }
};

class DepositoFresco : public Deposito
{
private:
    std::string id_deposito;
    int number_paletes;
    int number_produtos;
    int capacity;
    int area;
    int distance;

public:
    DepositoFresco(int type_deposito, std::string id_deposito, int number_paletes, int number_produtos, int capacity, int area, int distance):
        Deposito(type_deposito),
        id_deposito(id_deposito),
        number_paletes(number_paletes),
        number_produtos(number_produtos),
        capacity(capacity),
        area(area),
        distance(distance)
    {}

    void testFunc()
    {
        std::cout << "You called a depositoFresco testFunc with id: " << this->id_deposito << std::endl;
    }

};

int main(int argc, char* argv[])
{
    int valormaximo = 100;
    int valorminimo = 0;
    int number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    std::vector<Deposito*> list_depositos;
    int j;
    for (j = 0; j < number_depositosf; ++j) {

        int type_deposito = 0;
        std::stringstream ss;
        ss << j;
        std::string numStr(ss.str());
        std::string id_deposito = "df" + numStr;
        int number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;

        DepositoFresco* deposito = new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance);
        list_depositos.push_back(deposito);
    }

    if(list_depositos.size() > 1)
    {
        //Retrieve and cast
        DepositoFresco* retrieved_depositofresco = static_cast<DepositoFresco*>(list_depositos[0]);
        retrieved_depositofresco->testFunc();//Calls DepositoFresco::testFunc();

        //Simple retrieve
        Deposito* retrieved_deposito = list_depositos[0];
        retrieved_deposito->testFunc(); //Calls Deposito::testFunc()
    }
}
#包括 #包括 #包括 类别存款 { 私人: int型存款; 公众: 存款(整数类型存款): 类型存款(类型存款) {} void testFunc() {
std::cout list_depositos是什么类型的?您发布的大多数代码对于您的要求都是不必要的。我不知道您想做什么,但有几个问题:
DepositorResco id\u deposito=new DepositorResco
您正在为非指针变量分配指针。同样,在第一个代码段中,您
push_back()
一个指针,在你用非指针做这件事的那一秒。我想你需要一个std::map。所以你可以做
list_depositos[id_deposito]=new depositorResco(…);
list list list_depositorshanks,我会尽力去做的。我认为你需要做的就是包含。
list_depositos[id_deposito] = new DepositoFresco(...);
list_depositos[id_deposito]
#include <random>
#include <string>
#include <iostream>
#include <sstream>

class Deposito
{
private:
    int type_deposito;
public:
    Deposito(int type_deposito):
        type_deposito(type_deposito)
    {}

    void testFunc()
    {
        std::cout << "You called a deposito testFunc with type: " << this->type_deposito << std::endl;
    }
};

class DepositoFresco : public Deposito
{
private:
    std::string id_deposito;
    int number_paletes;
    int number_produtos;
    int capacity;
    int area;
    int distance;

public:
    DepositoFresco(int type_deposito, std::string id_deposito, int number_paletes, int number_produtos, int capacity, int area, int distance):
        Deposito(type_deposito),
        id_deposito(id_deposito),
        number_paletes(number_paletes),
        number_produtos(number_produtos),
        capacity(capacity),
        area(area),
        distance(distance)
    {}

    void testFunc()
    {
        std::cout << "You called a depositoFresco testFunc with id: " << this->id_deposito << std::endl;
    }

};

int main(int argc, char* argv[])
{
    int valormaximo = 100;
    int valorminimo = 0;
    int number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    std::vector<Deposito*> list_depositos;
    int j;
    for (j = 0; j < number_depositosf; ++j) {

        int type_deposito = 0;
        std::stringstream ss;
        ss << j;
        std::string numStr(ss.str());
        std::string id_deposito = "df" + numStr;
        int number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;

        DepositoFresco* deposito = new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance);
        list_depositos.push_back(deposito);
    }

    if(list_depositos.size() > 1)
    {
        //Retrieve and cast
        DepositoFresco* retrieved_depositofresco = static_cast<DepositoFresco*>(list_depositos[0]);
        retrieved_depositofresco->testFunc();//Calls DepositoFresco::testFunc();

        //Simple retrieve
        Deposito* retrieved_deposito = list_depositos[0];
        retrieved_deposito->testFunc(); //Calls Deposito::testFunc()
    }
}