简体   繁体   English

QString fromStdString静态函数而不是构造函数

[英]QString fromStdString static function instead of constructor

What is the reason for the choice of API below: 下面选择API的原因是什么:

QString QString::fromStdString(const std::string & str) [static]

Why is there a need for calling a static function when we could have a constructor as follows: 当我们有一个构造函数时,为什么需要调用静态函数,如下所示:

QString::QString(const std::string & str)

There are similar constructors for the c string representation (char*), std::string being the exception. c字符串表示(char *)有类似的构造函数,std :: string是异常。 What is the reason for that? 这是什么原因?

Qt predates explicit constructors, and it can't break its existing clients gratuitously. Qt早于explicit构造函数,并且无法无偿地破坏现有客户端。 One other reason would be that std::string and QString are not necessarily in the same encoding/codepage and the static methods make explicit that some more expensive conversion is happening. 另一个原因是std::stringQString不一定在同一个编码/代码页中,而静态方法明确表示会发生一些更昂贵的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM