const在C ++中的优点? [重复]

时间:2022-10-29 12:05:26

What are the advantages of const in C++ (and C) for the uninitiated?

对于不熟悉的人来说,C ++(和C)中const的优点是什么?

6 个解决方案

#1


Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.

Const对于传递给函数的指针或引用特别有用 - 它是一种即时可理解的“API契约”,其功能不会改变传递的对象。

See also: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4

另见:http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4

#2


When used as a const reference in a function, it lets the caller know that the thing being passed in won't be modified.

当在函数中用作const引用时,它使调用者知道传入的东西不会被修改。

void f(Foo &foo, const Bar &bar) { ... }

In this case the caller will know that the foo might be modified, but the bar will not. The compiler will enforce this when compiling the body of f(), so that bar is never modified and never passed on to another function that might modify it.

在这种情况下,调用者将知道foo可能被修改,但条形码不会。编译器在编译f()的主体时会强制执行此操作,以便永远不会修改bar并且永远不会将其传递给可能修改它的另一个函数。

All of the above safeguards can be bypassed using const_cast, which is why such a cast is considered "dangerous" (or at the very least, suspicious).

所有上述安全措施都可以使用const_cast进行绕过,这就是为什么这样的强制转换被认为是“危险的”(或者至少是可疑的)。

#3


Seems pretty obvious - it keeps you from modifying things that shouldn't be modified.

看起来非常明显 - 它可以防止您修改不应修改的内容。

Edit: for more guidance, always look to Herb Sutter.

编辑:有关更多指导,请始终关注Herb Sutter。

#4


I'm not so convinced on the "safety" aspect of const (there's pros and cons)....however, what it does allow is certain syntax that you can't do without const

我不太相信const的“安全”方面(有利有弊)....但是,它允许的是某些语法,你不能没有const

void blah(std::string& x)
{}

can only take a std::string object... However if you declare it const :-

只能取一个std :: string对象...但是如果你声明它是const: -

void blah(const std::string& x) {}

you can now do

你现在可以做

blah("hello");

which will call the appropriate constructor to make a std::string

它将调用适当的构造函数来生成std :: string

#5


Also, by using const, you state your intention for the use of a variable or parameter. It is a good programming practice ("C/C++ Coding Style & Standards", item 2.3).

此外,通过使用const,您声明了使用变量或参数的意图。这是一个很好的编程实践(“C / C ++编码风格和标准”,第2.3项)。

Also by avoiding using #define's to define constants and using const's, you increase the type-safety of your code. C++ Programming Practice Guidelines - item 2.1

另外,通过避免使用#define来定义常量和使用const,可以提高代码的类型安全性。 C ++编程实践指南 - 第2.1项

#6


The contract aspect of const is also important to the compiler's optimizer. With certain const declarations, optimizations involving loop invariants are easier for the optimizer to spot. This is why const_cast is truly dangerous.

const的契约方面对编译器的优化器也很重要。对于某些const声明,优化器更容易发现涉及循环不变量的优化。这就是const_cast真正危险的原因。

#1


Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.

Const对于传递给函数的指针或引用特别有用 - 它是一种即时可理解的“API契约”,其功能不会改变传递的对象。

See also: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4

另见:http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4

#2


When used as a const reference in a function, it lets the caller know that the thing being passed in won't be modified.

当在函数中用作const引用时,它使调用者知道传入的东西不会被修改。

void f(Foo &foo, const Bar &bar) { ... }

In this case the caller will know that the foo might be modified, but the bar will not. The compiler will enforce this when compiling the body of f(), so that bar is never modified and never passed on to another function that might modify it.

在这种情况下,调用者将知道foo可能被修改,但条形码不会。编译器在编译f()的主体时会强制执行此操作,以便永远不会修改bar并且永远不会将其传递给可能修改它的另一个函数。

All of the above safeguards can be bypassed using const_cast, which is why such a cast is considered "dangerous" (or at the very least, suspicious).

所有上述安全措施都可以使用const_cast进行绕过,这就是为什么这样的强制转换被认为是“危险的”(或者至少是可疑的)。

#3


Seems pretty obvious - it keeps you from modifying things that shouldn't be modified.

看起来非常明显 - 它可以防止您修改不应修改的内容。

Edit: for more guidance, always look to Herb Sutter.

编辑:有关更多指导,请始终关注Herb Sutter。

#4


I'm not so convinced on the "safety" aspect of const (there's pros and cons)....however, what it does allow is certain syntax that you can't do without const

我不太相信const的“安全”方面(有利有弊)....但是,它允许的是某些语法,你不能没有const

void blah(std::string& x)
{}

can only take a std::string object... However if you declare it const :-

只能取一个std :: string对象...但是如果你声明它是const: -

void blah(const std::string& x) {}

you can now do

你现在可以做

blah("hello");

which will call the appropriate constructor to make a std::string

它将调用适当的构造函数来生成std :: string

#5


Also, by using const, you state your intention for the use of a variable or parameter. It is a good programming practice ("C/C++ Coding Style & Standards", item 2.3).

此外,通过使用const,您声明了使用变量或参数的意图。这是一个很好的编程实践(“C / C ++编码风格和标准”,第2.3项)。

Also by avoiding using #define's to define constants and using const's, you increase the type-safety of your code. C++ Programming Practice Guidelines - item 2.1

另外,通过避免使用#define来定义常量和使用const,可以提高代码的类型安全性。 C ++编程实践指南 - 第2.1项

#6


The contract aspect of const is also important to the compiler's optimizer. With certain const declarations, optimizations involving loop invariants are easier for the optimizer to spot. This is why const_cast is truly dangerous.

const的契约方面对编译器的优化器也很重要。对于某些const声明,优化器更容易发现涉及循环不变量的优化。这就是const_cast真正危险的原因。