简体   繁体   中英

c++ meaning of the use of const in the signature

Please help me understand the following signature:

err_type funcName(const Type& buffer) const;

so for the first const, does that mean the contents of Type cannot change or that the reference cannot change?

secondly, what does the second const mean? I don't really even have a hint.

Thanks in advance, jbu

The second const means the method can be called on a const object.

Consider this 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
}

Furthermore, a const method is not allowed to change any members of the object (unless the member is specifically marked at 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
}

Yes, the first const means that buffer can not change.

The second const implies that this is a class' member function, and will not change the object ( this ).

这将比我们大多数人解释得更好,所以我不会重复: http//www.parashift.com/c++-faq-lite/const-correctness.html

Directly: It means the const applies to instances on which that method is called. You may not call it on a non-const object.

The const rule: The const keyword modifies the thing to its left , unless there is nothing, in which case it modifies the thing to its right.

Thus, the type "const int" is a special case and means the same as "int const".

When you apply the rule to methods you get "<method> const". (It's something of a stretch, but still helpful to understand this way.)

Other examples ("read from the inside-out"), where "id" is the identifier, but may be removed without changing the types:

  • int const* id = pointer to constant int
  • int* const id = constant pointer to int
  • int const id[5] = array of 5x constant int
  • int (*const id)[5] = constant pointer to array of 5x int

More commonly, T const& and T const* are used by some people, as it maintains consistency by not requiring a special case. I additionally prefer it because the type at the start is often more helpful than knowing it's constant.

The other answers are correct, but it has a more significant meaning. It is a contract for your class that says "by calling this function, you will not change the state of your object". People interpret this differently, some view it as the object will not change in appearance to the caller, while others view it as the object will not change at all if you call this function.

The first const is a gurantee that the function will not change buffer , and thus can be passed a const buffer.

The second const is only meaningful when used as a method for a class. It is a gurantee that calling the method will not have any side effects on the class that contains the method. Essentially a gurantee that this method can be safely called on const objects.

While you've gotten some good answers, I think it's worth pointing out that a class can contain mutable member variables. A mutable member is one that you're always allowed to modify, even when it's part of a const object. These are typically used for things like memoizing and caching, where the variable can be modified without affecting the user-visible state of the object as a whole.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM