C++初学者:如果使用“const”,通过引用使用变量有什么意义?
我想知道这个函数声明中的逻辑:
CMyException (const std::string & Libelle = std::string(),...
通过引用使用变量有什么意义?通常,每当变量可能在内部修改时,您都会通过引用传递变量...因此,如果您使用关键字const
,这意味着它永远不会被修改。
这是矛盾的。
有人可以向我解释一下吗?
I was wondering about the logic in this function declaration:
CMyException (const std::string & Libelle = std::string(),...
What is the point of using a variable by reference? Usually you pass a variable by reference whenever it may be modified inside... so if you use the keyword const
this means it'll never be modified.
This is contradictory.
May someone explain this to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
实际上引用是用来避免不必要的对象复制。
现在,要了解为什么使用
const
,请尝试以下操作:它将给出编译错误。这是因为表达式
std::string()
创建了一个无法绑定到非常量引用的临时对象。但是,临时变量可以绑定到const
引用,这就是需要const
的原因:现在回到代码中的构造函数:
它为参数设置默认值。默认值是从临时对象创建的。因此,您需要
const
(如果您使用引用)。使用 const 引用还有一个优点:如果您有这样的构造函数,那么您可以引发如下异常:
它创建一个
std::string
字符串文字"error"
之外,并且该临时值绑定到const
引用。Actually reference is used to avoid unnecessary copy of the object.
Now, to understand why
const
is used, try this:It will give compilation error. It is because the expression
std::string()
creates a temporary object which cannot be bound to non-const reference. However, a temporary can be bound toconst
reference, that is whyconst
is needed:Now coming back to the constructor in your code:
It sets a default value for the parameter. The default value is created out of a temporary object. Hence you need
const
(if you use reference).There is also an advantage in using const reference : if you've such a constructor, then you can raise exception like this:
It creates a temporary object of type
std::string
out of the string literal"error"
, and that temporary is bound to theconst
reference.有些参数可能会使用相当多的内存。如果您将参数作为值传递,它将被复制并将副本传递给方法。
将它们作为引用传递只会将指针传递给更快的方法,并节省复制的内存。
Some arguments might use quite some memory. If you pass an argument as value it will be copied and the copy will be passed to the method.
Passing them as reference will only pass the pointer to the method which is faster and saves the memory for the copy.
例如,您只能使用 const 引用参数来做到这一点:
想一想,然后它就会变得清晰。
For example you can only do this with const reference argumernt:
Think about it and then it will become clear.
通过 const 引用传递基本类型(
int
、char
、..)是没有意义的。即使对于std::string
我也认为它是不需要的。但是,较大的结构或类在按值传递时需要副本,因此存在开销。 const 引用模拟按值传递的行为(外部变量不会被修改),但也可以防止额外的复制。
Passing primitive types (
int
,char
,..) by const reference doesn't make sense. Even forstd::string
I would argue it's not needed.However, larger structures or classes require a copy when passing by value, so there's overhead there. A
const
reference simulates the behavior of passing by value (the outside variable isn't modified), but also prevents the extra copy.不。并不总是需要每当您传递任何变量作为引用时,它只是为了可以在内部修改它。如果变量按值传递,则每当调用该函数时都会创建该变量的副本。
另一方面,引用变量使用相同的对象,并且本质上仅传递内存地址(与使用 std::string* 相同,但不能使用空内存地址)。因此,当您执行 const std::string& 之类的操作时x,你的意思是:
如果你考虑一下,当你使用引用时使用
const
是有意义的,而不是其他情况。如果您要复制我传递的变量然后修改它,我真的不在乎。但是,如果您不打算修改我传递的那个对象(因为您将使用同一个对象),那么我很高兴知道这样一个事实:我可以在此下进一步严格定义我的应用程序的流程保证。No. It is not always necessary that whenever you pass any variable as reference it is only so that it may be modified inside. If the variable is passed by value then a copy of the variable is made whenever that function is called.
A reference variable on the other hand, uses the same object and essentially passes only the memory address (same as using std::string*, but with the exception that you cannot use a null memory address). So, when you do something like
const std::string& x
, what you're saying is:If you think about it, using
const
makes sense when you're working with references and not otherwise. If you are making a copy of a variable that I pass and then modifying it, I couldn't really care less. However, it would be really nice to know for a fact that if you're not going to modify the very object I pass (as you will be using the very same object), I can strictly further define the process of my application under this guarantee.我通常将所有内容都作为 const 传递 - 我从不修改参数,通过原始类型的值,通过自定义类型的引用。在大多数情况下,按基本类型的值传递更有效 - 考虑无符号短整型 - 按值 2 个字节,按引用 4-8 个字节,具体取决于指针大小。
I generally pass everything as const - I never modify the parameters, by value for primitive types, by reference for custom types. Passing by value for primitive types is more efficient in most of the cases - consider an unsigned short - by value 2 bytes, by reference 4-8 bytes depending on the pointer size.