[英]Why can I initialize string literal as const char* and QString but not QString*?
I am relatively new to qt and C++, and I am having trouble understanding why I can declare the following in a header file: 我对qt和C ++相对较新,并且在理解为什么可以在头文件中声明以下内容时遇到了麻烦:
const char* CONSIGNMENT_TAG_DONATE = "DTag";
or 要么
QString CONSIGNMENT_TAG_DONATE = "DTag";
or 要么
const QString CONSIGNMENT_TAG_DONATE = "DTag";
But when I try to use QString* 但是当我尝试使用QString *时
QString* CONSIGNMENT_TAG_DONATE = "DTag";
or 要么
const QString* CONSIGNMENT_TAG_DONATE = "DTag";
I get an error saying cannot initialize a member subobject of type 'QString*' with an lvalue of type 'const char[5]'
. 我收到一个错误消息,指出
cannot initialize a member subobject of type 'QString*' with an lvalue of type 'const char[5]'
。 It's my understanding that string literals are of a type const char*
so I get why that works. 据我了解,字符串文字是
const char*
类型的,所以我明白了为什么它可以工作。 But why is assigning it to QString
valid but QString*
not valid? 但是,为什么将其分配给
QString
有效却QString*
无效呢? I'm sure this is a fairly basic concept of C++, I am just trying to understand it. 我确定这是C ++的基本概念,我只是想了解它。
A QString*
is a pointer to QString
. QString*
是指向QString
的指针。 That means it can hold the address of a QString
object. 这意味着它可以保存
QString
对象的地址。 You cannot (or at least are not supposed to most of the time) store addresses of non- QString
in a QString*
. 您不能(或至少在大多数情况下不应该)将非
QString
地址存储在QString*
。
const char* CONSIGNMENT_TAG_DONATE = "DTag";
works because the "DTag"
(which is of type const char[5]
) decays to const char*
, thus the assignment makes sense. 之所以起作用,是因为
"DTag"
(类型为const char[5]
)衰减为const char*
,因此分配是有意义的。
QString CONSIGNMENT_TAG_DONATE = "DTag";
works because QString
s have a non-explicit constructor taking const char*
, so the literal can again decay to const char*
which is then used to construct the QString
. 之所以起作用,是因为
QString
具有一个采用const char*
的非显式构造const char*
,因此文字可以再次衰减为const char*
,然后将其用于构造QString
。
QString* CONSIGNMENT_TAG_DONATE = "DTag";
is just nonsense, see first paragraph. 只是胡说八道,见第一段。
Using a QString*
instead of a plain QString
is most likely not helpful for your usecase. 使用
QString*
代替普通的QString
很可能对您的用例没有帮助。
C++ doesn't know how to convert a char array to a pointer to QString
. C ++不知道如何将char数组转换为指向
QString
的指针。
const char* CONSIGNMENT_TAG_DONATE = "DTag";
Constant char array to const char *
- permitted 常量char数组为
const char *
-允许
QString CONSIGNMENT_TAG_DONATE = "DTag";
Implicit conversion using the QString::QString(const char *str)
constructor 使用
QString::QString(const char *str)
构造QString::QString(const char *str)
隐式转换
QString* CONSIGNMENT_TAG_DONATE = "DTag";
Constant char array to QString *
- how does this work? QString *
常量char数组QString *
-这是如何工作的? There are no rules to make such a conversion. 没有进行此类转换的规则。 It may be simple enough conceptually, but without the actual code to enable it, it will not work.
从概念上讲,它可能足够简单,但是如果没有启用它的实际代码,它将无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.