1

我们有一个用 C++/Qt 和 Axis2C 编写的基于 SOAP 的客户端-服务器。由于 Axis2C 的 C 特性,它包含许多老式的 C 样式结构(通常它们描述自定义数据的原始数组)。如何在使用 Axis2C 的代码中最小化 C 的使用?支持这些自定义 C 结构很痛苦,因为它需要赋值运算符、c-tors、d-tors 的准确性。基于 Qt 的结构不那么冗长。

4

1 回答 1

1

我猜您特别关注要使用哪些数据类型,而不是老式的 C(不是 C++)数据类型。这些数据类型是 C++ 标准容器 ( http://www.cplusplus.com/reference/stl/ ),它们随您的编译器一起提供并且始终可用。这些容器的 Qt 实现也可用(http://doc.qt.io/qt-5/containers.html)。

选择哪一个取决于很多因素。下面我展示了如何使用 stl 执行此操作的简化示例。所以我认为你将不得不编写一种将 c 数据类型转换为 C++/Qt 数据类型的包装器。"std::vector" 是一种容器类型,通常可以很好地替代 c 样式数组。

int32_t main ()
{
    int arraySize = 10;
    int* pCArray = new int [arraySize];

    for ( int idx = 0; idx < arraySize; ++idx )
    {
        pCArray [idx] = idx + 100;
    }

    for ( int idx = 0; idx < arraySize; ++idx )
    {
        std::cout << pCArray [idx] << std::endl;
    }

    std::cout << "-------------------------" << std::endl;

    std::vector<int> array;
    array.assign ( pCArray, pCArray + arraySize );

    delete pCArray;

    for ( int idx = 0; idx < arraySize; ++idx )
    {
        std::cout << array [idx] << std::endl;
    }

    return 0;
}

由于“数组”会自动删除(顺便说一句,甚至不会编译) ,因此无需delete array在此示例的末尾调用。delete array

于 2016-08-22T10:45:06.920 回答