qstring 方法toLocal8Bit().data()的注意事项
qstring转const char*传入函数的区别
方式1.
const char* testChar = str.toLocal8Bit().data(); testFun(testChar);
方式2.
testFun(str.toLocal8Bit().data());
两种方式有何区别?
以下是继承qsting转换的中间对象QByteArray后的测试结果
结论:
- 方式1不能直接使用 中间变量需要需要用临时变量赋值
- 方式2在debug与release都满足预期EByteArray在函数调用完成后才析构
参考写法
//方法1 QByteArray byteData = str.toLocal8Bit(); const char* testChar = byteData.data(); testFun(testChar); //方法2 testFun(str.toLocal8Bit().data());
,测试代码如下:
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QByteArray>
class EByteArray :public QByteArray
{
public:
EByteArray(const char* charData, int size = -1) :QByteArray(charData,size)
{
qDebug() << "EByteArray create";
}
~EByteArray()
{
qDebug() << "EByteArray delete";
}
};
class TestClass
{
public:
EByteArray getBytes()
{
EByteArray aa("aaa");
return aa;
}
};
void testFun(const char* test)
{
qDebug() <<"print in Fun"<< test;
}
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
{//常规使用方法 类似在函数输入中调用 str.toLocal8Bit().data()
qDebug() << "--------------start test1";
TestClass t1;
qDebug() << "start fun";
testFun(t1.getBytes().data());
qDebug() << "--------------end test1";
}
{//变量转接方法 将char*获取到再传入函数
qDebug() << "--------------start test2";
TestClass t1;
const char* testChar = t1.getBytes().data();
qDebug() << "start fun";
testFun(testChar);
qDebug() << "--------------end test2";
}
{//常规使用方法 函数输入中调用 str.toLocal8Bit().data()
qDebug() << "--------------start test3";
QString str = "aaa";
qDebug() << "start fun";
testFun(str.toLocal8Bit().data());
qDebug() << "--------------end test3";
}
{//变量转接方法 将char*获取到再传入函数
qDebug() << "--------------start test4";
QString str = "aaa";
const char* testChar = str.toLocal8Bit().data();
qDebug() << "start fun";
testFun(testChar);
qDebug() << "--------------end test4";
}
{//变量转接方法 QByteArray增加临时变量暂存 将char*获取到再传入函数
qDebug() << "--------------start test5";
QString str = "aaa";
QByteArray byteData = str.toLocal8Bit();
const char* testChar = byteData.data();
qDebug() << "start fun";
testFun(testChar);
qDebug() << "--------------end test5";
}
return a.exec();
}
运行结果
release
--------------start test1 start fun EByteArray create print in Fun aaa EByteArray delete --------------end test1 --------------start test2 EByteArray create EByteArray delete start fun print in Fun aaa --------------end test2 --------------start test3 start fun print in Fun aaa --------------end test3 --------------start test4 start fun print in Fun start fun --------------end test4 --------------start test5 start fun print in Fun aaa --------------end test5
debug
--------------start test1 start fun EByteArray create EByteArray delete print in Fun aaa EByteArray delete --------------end test1 --------------start test2 EByteArray create EByteArray delete EByteArray delete start fun print in Fun ???????? --------------end test2 --------------start test3 start fun print in Fun aaa --------------end test3 --------------start test4 start fun print in Fun ???????????????????????????????????????????? --------------end test4 --------------start test5 start fun print in Fun aaa --------------end test5
专注机器视觉,halcon c# c++联合开发

浙公网安备 33010602011771号