C++初学者:如果使用“const”,通过引用使用变量有什么意义?

发布于 2025-01-05 07:21:22 字数 255 浏览 1 评论 0原文

我想知道这个函数声明中的逻辑:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失退 2025-01-12 07:21:22

实际上引用是用来避免不必要的对象复制。

现在,要了解为什么使用 const,请尝试以下操作:

std::string & x= std::string(); //error

它将给出编译错误。这是因为表达式 std::string() 创建了一个无法绑定到非常量引用的临时对象。但是,临时变量可以绑定到 const 引用,这就是需要 const 的原因:

const std::string & x = std::string(); //ok

现在回到代码中的构造函数:

CMyException (const std::string & Libelle = std::string());

它为参数设置默认值。默认值是从临时对象创建的。因此,您需要const(如果您使用引用)。

使用 const 引用还有一个优点:如果您有这样的构造函数,那么您可以引发如下异常:

throw CMyException("error"); 

它创建一个 std::string 字符串文字 "error" 之外,并且该临时值绑定到 const 引用。

Actually reference is used to avoid unnecessary copy of the object.

Now, to understand why const is used, try this:

std::string & x= std::string(); //error

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 to const reference, that is why const is needed:

const std::string & x = std::string(); //ok

Now coming back to the constructor in your code:

CMyException (const std::string & Libelle = std::string());

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:

throw CMyException("error"); 

It creates a temporary object of type std::string out of the string literal "error", and that temporary is bound to the const reference.

魂ガ小子 2025-01-12 07:21:22

有些参数可能会使用相当多的内存。如果您将参数作为值传递,它将被复制并将副本传递给方法。

将它们作为引用传递只会将指针传递给更快的方法,并节省复制的内存。

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.

〃安静 2025-01-12 07:21:22

例如,您只能使用 const 引用参数来做到这一点:

CMyException("foo");

想一想,然后它就会变得清晰。

For example you can only do this with const reference argumernt:

CMyException("foo");

Think about it and then it will become clear.

失而复得 2025-01-12 07:21:22

通过 const 引用传递基本类型(intchar、..)是没有意义的。即使对于 std::string 我也认为它是不需要的。

但是,较大的结构或类在按值传递时需要副本,因此存在开销。 const 引用模拟按值传递的行为(外部变量不会被修改),但也可以防止额外的复制。

Passing primitive types (int, char,..) by const reference doesn't make sense. Even for std::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.

策马西风 2025-01-12 07:21:22

不。并不总是需要每当您传递任何变量作为引用时,它只是为了可以在内部修改它。如果变量按值传递,则每当调用该函数时都会创建该变量的副本。

另一方面,引用变量使用相同的对象,并且本质上仅传递内存地址(与使用 std::string* 相同,但不能使用空内存地址)。因此,当您执行 const std::string& 之类的操作时x,你的意思是:

1. The passed argument will not be copied. The same object will be used as in memory.
2. The function will absolutely not modify the object that it is handling.

如果你考虑一下,当你使用引用时使用 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:

1. The passed argument will not be copied. The same object will be used as in memory.
2. The function will absolutely not modify the object that it is handling.

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.

少女净妖师 2025-01-12 07:21:22

我通常将所有内容都作为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文