为什么 QFile( "./test" ) 工作,尽管 [类 QFile] 没有 QFile(char* s) 构造函数

Why QFile("./test") work though [Class QFile] has no QFile(char* s) constructor

本文关键字:QFile 没有 char 构造函数 test 工作 为什么 尽管      更新时间:2023-10-16

我检查了QT-Help,我发现类QFile有以下构造函数:

QFile()
QFile(const QString &name)
QFile(QObject *parent)
QFile(const QString &name, QObject *parent)

对于QFile类没有QFile(char *)但是QFile("/home/mythicsr/test");是可以的,为什么?

考虑以下语句:

QFile("/home/mythicsr/test");

它导致在底层创建QString类型的临时对象,从而调用构造函数QFile(const QString &name)
正如@meetaig在评论中提到的,这是可能的,因为:

QString支持使用char*

初始化

或者更好,正如@Slava - const char *在本例中所指出的那样。