C ++ value of using const in signature

Please help me understand the following signature:

err_type funcName(const Type& buffer) const;

so for the first const does this mean that the contents of the Type cannot change or that the link cannot change?

secondly, what does the second constant mean? I don’t even have a hint.

Thanks in advance, JBU

+3
source share
7 answers

The second constant means that the method can be called for the const object.

Consider the following example:

class foo
{
public:
    void const_method() const;
    void nonconst_method();
};

void doit()
{
    const foo f;

    f.const_method();      // this is okay
    f.nonconst_method();   // the compiler will not allow this
}

In addition, the const method does not allow changes to any elements of the object (unless the element is marked as mutable):

class foo
{
public:
    void const_method() const;

private:
    int r;
    mutable int m;
};

void foo::const_method() const
{
    m = 0; // this is okay as m is marked mutable
    r = 0; // the compiler will not allow this
}
+18
source

Yes, the first constmeans that bufferit cannot change.

const , - (this).

+5

: , , ​​ . .

const: const , , .

, "const int" , "int const".

, "<method> const". ( - , .)

( "read from the out-out" ), "id" - , :

  • int const* id & emsp; = int
  • int* const id & emsp; = int
  • int const id[5] & emsp; = 5x int
  • int (*const id)[5] & emsp; = 5x int

T const& T const* , , . , , .

+3

, . , : " , ". -, , , , , .

+2

const , buffer , , const.

const . , , . , , const .

0

, , , - mutable. mutable - , , const. , memoizing , .

0

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


All Articles