[英]C++ constructor with a const argument
I just wondered what's the difference between having a constructor with const char* c
or char* const c
as a parameter? 我只是想知道将构造函数与
const char* c
或char* const c
作为参数之间的区别是什么? If I swap char*
and const
in header and source file it isn't a problem but when I mix them it won't compile, saying there is no such overloaded constructor. 如果我在头文件和源文件中交换
char*
和const
这不是问题,但是当我混合它们时它将无法编译,说没有这样的重载构造函数。
When I use my own class for a copy constructor the compiler doesn't seem to bother if I have MyClass const &other
and const MyClass &other
mixed.. 当我使用自己的类作为复制构造函数时,如果我有
MyClass const &other
和const MyClass &other
mixed,编译器似乎不会打扰。
Can anyone enlighten me please? 请问有人可以开导我吗? :)
:)
This record const char *c
declares pointer c that points to an object of type const char
. 此记录
const char *c
声明指向c的类型为const char
的对象。 That is using this pointer you may not change the object that refered to by the pointer. 即使用此指针,您可能无法更改指针所引用的对象。
This record char * const c
declares const
pointer c that points to a non-const object of type char
. 此记录
char * const c
声明const
指针c,指向char
类型的非const对象。 You may not change the pointer itself but you may change the object refered to by the pointer. 您可能无法更改指针本身,但您可以更改指针所引用的对象。
There is a difference between declaring pointers and references. 声明指针和引用之间存在差异。 Though there is used term "constant reference" strictly speaking references themselves may not be constant.
虽然术语“常量引用”严格来说,引用本身可能不是一成不变的。 According to the C++ grammar
根据C ++语法
ptr-operator:
* attribute-specifier-seqopt cv-qualifier-seq
& attribute-specifier-seqopt
references do not contain cv-qualifier-seq. 引用不包含cv-qualifier-seq。 So for example this record
所以例如这个记录
Airplane & const other
will be incorrect and the compiler shall issue an error. 将是不正确的,编译器将发出错误。 As for these records
至于这些记录
const Airplane & other
Airplane const & other
then they are equivalent the same way as equivalent the recirds below 然后它们与下面的recirds相同
const char *c
char const *c
Think of the *
as a "fence". 将
*
视为“围栏”。 What's before it in the declaration defines the type that the pointer refers to . 什么是之前的声明定义指针指的类型。 What's after the
*
defines the pointer itself. *
之后的内容定义了指针本身。 For this case, it's easiest to read things backwards (translating *
as "pointer to"), so char const *t
is read (from back to front) as: "t is a pointer to a const char". 对于这种情况,最容易向后读取事物(将
*
转换为“指向”),因此char const *t
被读取(从后到前):“t是指向const char的指针”。 Likewise, char *const t
is read as: "t is a const pointer to a char." 同样,
char *const t
读作:“t是指向char的const指针。”
char *const
means the pointer itself is const
(can't be modified), but the char(s) it points at can be modified. char *const
装置指针本身是const
(不能被修改),但炭(多个)它在 可以被修改指向。 char const *
or const char *
are equivalent to each other. char const *
或const char *
彼此相同。 Both mean the pointer itself can be modified, but what it points at cannot. Substituting a different type for char
doesn't change that basic idea. 用不同类型代替
char
并不会改变这个基本思想。
The same is true with a reference: const T &
means a reference to a const T
, so you can't modify the T
that it refers to. 引用也是如此:
const T &
表示对const T
的引用,因此您无法修改它引用的T
In the case of a reference, you can't modify the reference itself to refer to a different object, so T & const
doesn't make any real sense (and isn't allowed). 在引用的情况下,您不能修改引用本身以引用不同的对象,因此
T & const
没有任何实际意义(并且不允许)。
const
适用于其左侧的类型(如果左侧没有任何内容,则适用于其右侧),因此const char *
和char const *
相同并且意味着字符不能更改,但指针可以,而char * const
表示字符可以更改,但指针不能
The order in which const and char* are written actually make a difference. 写入const和char *的顺序实际上有所不同。 If you have
const char*
in your header but char* const
in your source file, you will get an error because the compiler won't regard these as the same. 如果你的头文件中有
const char*
但源文件中有char* const
,则会出现错误,因为编译器不会将它们视为相同。
const char* p
means that p points to a char that is constant. const char* p
表示p指向一个常量的char。 We can alter where p points to, but we can never alter the contents stored at p. 我们可以改变p指向的位置,但我们永远不能改变存储在p的内容。
char* const p
means that p is a constant pointer and is pointing to a char. char* const p
表示p是一个常量指针,指向一个char。 We can alter the contents stored at p, but we cannot alter where p points to. 我们可以改变存储在p的内容,但是我们不能改变p指向的位置。
const char* const p
means that p is a constant pointer to a constant char. const char* const p
表示p是指向常量char的常量指针。 We cannot alter where p points to nor can we alter the contents at p. 我们不能改变p指向的位置,也不能改变p处的内容。 Hope this is helpful!
希望这有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.