[英]different between const string &name and string name in constructor for c++
Person(const string &name)
{
mName=name;
}
Person(string name)
{
mName=name;
}
mName is a private member variable mName是一个私有成员变量
So I made a class called Person. 所以我创建了一个名为Person的类。 I'm wondering whats the difference between const string &name and string name.
我想知道const字符串和名称与字符串名称之间的区别。 I have tried just putting string &name but it gave me an error message.
我试过把字符串和名称,但它给了我一个错误信息。
I know & is a reference so almost like an address? 我知道&是一个参考,所以几乎像一个地址? I'm guessing the const is needed because the reference is a constant?
我猜测需要const,因为引用是常量? Also, why doesn't just string &name work?
另外,为什么不只是字符串和名称工作?
This is the error message that I got 这是我收到的错误消息
candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [4]' to 'const Person' for 1st argument 候选构造函数(隐式复制构造函数)不可行:第一个参数没有从'const char [4]'到'const Person'的已知转换
candidate constructor not viable: no known conversion from 'const char [4]' to 'string &' (aka 'basic_string, allocator > &') for 1st argument Person(string &name) 候选构造函数不可行:没有已知的'const char [4]'转换为'string&'(又名'basic_string,allocator>&')第一个参数Person(string&name)
Option #1 - pass the argument by-value, with Person(string name)
: 选项#1 - 使用
Person(string name)
传递参数by-value:
string
constructor is called, and a temporary string
instance is created on the stack. 调用
string
构造函数,并在堆栈上创建临时string
实例。
Person
constructor is called. 调用
Person
构造函数。
string
destructor is called with the temporary instance. 使用临时实例调用
string
析构函数。
Option #2 - pass the argument by-reference, with Person(const string& name)
: 选项#2 - 使用
Person(const string& name)
传递参数by-reference:
A reference to the string
instance (its 4-byte or 8-byte address) is placed on the stack. 对
string
实例(其4字节或8字节地址)的引用放在堆栈上。
Person
constructor is called. 调用
Person
构造函数。
As you can see, option #2 is generally more efficient. 如您所见,选项#2通常更有效。
The compilation error is probably the result of something like Person x("abc")
: 编译错误可能是
Person x("abc")
:
The compiler first searches for a Person
constructor which takes the explicit type of the argument that you are passing. 编译器首先搜索
Person
构造函数,该构造函数采用您传递的参数的显式类型。 In this case, it is Person(const char[])
, which you have not defined. 在这种情况下,它是
Person(const char[])
,您尚未定义。
Then, the compiler searches for any other Person
constructor which takes a "convertable" type. 然后,编译器搜索任何其他采用“可转换”类型的
Person
构造函数。 Since operator const string&(const char[])
exists, the compiler can convert the argument "abc"
from const char[]
to const string&
. 由于
operator const string&(const char[])
存在,编译器可以将参数"abc"
从const char[]
为const string&
。 So if you define Person(const string& name)
, then your code is successfully compiled. 因此,如果您定义
Person(const string& name)
,那么您的代码将被成功编译。
Finally, the compiler explains for each Person
constructor that you've defined, why it is not a suitable candidate for your call. 最后,编译器会为您定义的每个
Person
构造函数解释为什么它不适合您的调用。 In this case, it tells you that there is no operator string&(const char[])
, which allows it to convert the argument "abc"
from const char[]
to string&
before calling your Person(string&)
constructor. 在这种情况下,它告诉您没有
operator string&(const char[])
,它允许它在调用Person(string&)
构造函数之前将参数"abc"
从const char[]
为string&
。
&name is a reference which means it is just another name for the string you pass in as an argument. &name是一个引用,这意味着它只是您作为参数传递的字符串的另一个名称。 This allows you to pass in the object itself, not just its value.
这允许您传入对象本身,而不仅仅是它的值。 You have to use const if you want to pass in a const.
如果要传入const,则必须使用const。 A const cannot be modified.
const不能修改。
&name is a reference to name, so the actual object is passed as a parameter, not a copy. &name是对name的引用,因此实际对象作为参数传递,而不是副本。 When you put const string &name, name is still being passed as a reference, but the const makes it so you cannot alter the actual object, just use it.
当你输入const字符串和名称时,name仍然作为引用被传递,但是const使得它不能改变实际的对象,只需使用它。
const string &name
与string name
之间的区别在于, const string &name
是对字符串的常量引用,此处在将其传递给函数时不会创建另一个副本,也不能在函数内部更改它,因为它是常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.