C ++ const semantics for reference

If the code has something like below:

func(const base& obj) 

what does semantics const mean? What is always here? Is obj a const reference to a non-const object or non-const reference to a const object ?

+4
source share
8 answers

obj is a reference to a const object.

There is no such thing as a "non-constant link" because the link cannot be changed to link to something else after its creation.

+5
source

There is no such thing as a “non-constant” link, that is, the link is always attached to the same object, and there is no way to change this. "const type &" means a reference to the type const.

+5
source

It is called a reference to const. You have “link access” to the transferred data, but you cannot change it.

+2
source

Without const, you cannot send a const object to this function. Therefore, it is always useful to add const. especially when you create a function for many users. Classic examples are setter functions.

 x->setXsth(sth& obj) // works only with non-const object. x->setXsth(const sth& obj) //works with const object and non-const. 
+2
source

obj is a reference to the constant base, so this means that you are not allowed to modify the reference object. It can be written as

 func(const base& obj) 

or

 func(base const & obj) 

Use the right right rule to read these types of ads, for this simple example, just read it on the right. More on this here:

http://www.codeproject.com/KB/cpp/complex_declarations.aspx

+1
source

obj - a permanent reference to the object (let the object be const or not const) is passed in the argument func()

if you write: func(B);

this means that you cannot change the contents of B inside the func() function

(where func(const base& obj) )

0
source

A few unsolicited answers / points of view: the constant modifier changes everything that is on the left side, with the exception of one construct you use (in this case, it changes everything that is immediately on the right). It becomes easier for me to always insert const immediately to the right of what I want to change, and read the instructions from right to left. This may not be the best way to do something, but it helps me keep it straight.

Examples:

 // these two statements are equivalent const int x = 5; // special case usage int const x = 5; // using the LHS syntax makes multiple consts easier to understand int const y = 6; int const * const x = &y; // x is a const pointer to const int // const can apply to pointers but not to references int const & const z = y; // redundant, references are always const 
0
source

As the other answers said, obj is a reference to a const base object. However, this does not mean that the object to which it refers is either of type base , or the object to which it refers is equal to const , just func cannot change obj through that . For instance:

 struct derived : base { ... }; derived d; func(d); 

is legal, and:

 bool other_func(const base& b, other_object& o) { base b_copy = b; o.foo(); return b_copy == b; } 

can return false if o has an internal non-constant reference to b (or something inside it) and o.foo() modifies b . This has practical implications for features such as

 std::string::operator=(const std::string& other); 

where a naive implementation might do the wrong thing for my_str = my_str .

-1
source

Source: https://habr.com/ru/post/1388367/


All Articles