创建多个同名的类对象?C++ [英] Creating multiple class objects with the same name? c++

查看:182
本文介绍了创建多个同名的类对象?C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个查询 MySQL 数据库的应用程序.

I'm making an application that is querying a MySQL database.

我希望将结果存储在映射中(具有对应的对):

I want the results of this to be stored in a map (which has a corresponding pair):

std::map<int, Car*> m_car;
typedef std::pair<int, Car*> m_car_pair;

汽车对象由8个参数组成,其中一个是car_id,所以首先我拉取汽车ID并将其用作键,然后我想将整个汽车对象存储为地图的值.(我知道这会让我将 car_id 存储两次,但目前我不介意).

The car object is made up of 8 parameters, one of which is car_id so firstly I pull the car ID and use it as the key then I want to store the entire car object as the value of the map. (I know this is casing me to be storing the car_id twice but for the moment I don't mind that).

无论如何这是我的查询代码:

Anyway here's my query code:

void DatabaseController::getAll(QString query_string)
{
    // Console log message
    std::cout << "Querying Database" << std::endl;

    /// Declare SQL query and pass function parameter 'query'
    QSqlQuery query(query_string);

    // execute the query
    query.exec();

    int count = 0;

    // While results are coming in
    while(query.next())
    {
        // Call car constructor passing all parameters
        Car car(query.value(count).toInt(), query.value(count+1).toString(), query.value(count+2).toString(), query.value(count+3).toString(),
            query.value(count+4).toInt(), query.value(count+5).toInt(), query.value(count+6).toInt(), query.value(count+7).toString());

        if (car.getcarID() != 0)
        {
            m_car_records.insert(m_car_pair(car.getcarID(), &car));
        }
    }

    std::cout << "Database query finished" << std::endl;

在此之后,我创建了一个快速测试函数来迭代地图并拉出所有 ID(地图键)并检查它们是否不同(即该函数有效).

After this I made a quick test function to iterate over the map and pull all of the ID's (map key) and check they were different (i.e. the function worked) and they were.

但这只是一个检查,我需要的是能够从汽车上调用应该在地图中的汽车对象上的辅助功能.所以我使用了相同的快速测试函数来迭代地图和 cout <<car.toString();(汽车类中一个简单的字符串函数):

But that was just a check what I needed was to be able to call the accessory functions from car on the car objects that should be in the map. So I used the same quick test function to iterate over the map and cout << car.toString(); (a simple to string function in the car class):

void DatabaseController::test()
{
    m_car_records_iterator = m_car_records.begin();

    for(unsigned int i = 0; i < m_car_records.size(); i++)
    {
        car *test = m_car_records_iterator->second;
        std::cout << test->toString() << std::endl;
        m_car_records_iterator++;
    }
}

这显示了正确数量的结果,但它们都是相同的,即已添加到地图中每个条目的汽车对象是相同的(查询找到的最后一条记录的值)

This showed the correct number of results however they all were the same i.e. the car object that has been added to every entry in the map is the same (the values of the last record that was found by the query)

我的问题是...

有什么方法可以使用我目前在查询中使用的这种结构,我可以在while循环中使用相同的名称创建这些类对象并将其添加到我的地图中,因为我当然不知道有多少结果正在返回并为每个对象声明一个类对象,但就目前而言,使用相同的名称只是每次都添加相同的名称,而不是实际替换值......至少我认为这就是正在发生的事情?

Is there any way that using this structure I currently have for my query I can create and add these class objects to my map within the while loop using the same name for each, because of course I can't know how many results are being returned and declare a class object for each one, but as it stands using the same name is just adding the same one every time not actually replacing the values... at least that's what I think is happening??

欢迎任何建议或想法(对不起,帖子太长)

Any advice or idea would be welcomed (sorry for the long post)

推荐答案

您遇到未定义行为.原因是你在映射中插入了一个指向局部变量的指针.

You are experiencing undefined behavior. The reason is that you insert a pointer to a local variable in the map.

getAll 的循环中,当循环从下一项开始时,car 变量不再有效.

In the loop in getAll, when the loop starts over on the next item the car variable is not valid any more.

我建议您查看 std::shared_ptr 为指针.

I suggest you look into std::shared_ptr for the pointers.

这篇关于创建多个同名的类对象?C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆