简体   繁体   中英

C++ const function with reference

In this function :

(Counter is the class in which the function operator++ is declared)

const Counter& operator++ ();

What means the const for my function ? I don't really understand the const keyword in combination with pointers or references !

This is an operator overload, but the declaration syntax is similar to a function. Your declaration can be split into 3 parts:

  • the return type: const Counter&
  • the function name: operator++
  • and the parameters type: () (no parameters)

So const Counter & tells you that the function will return a constant reference to a Counter object. A constant reference makes so that the object can't be modified.

For example:

Counter c1;
++(++c1); // invalid

Here (++c1) returns a const reference to a Counter object. This reference is incremented, but is invalid, because this reference can't be modified (it's constant).

It means that you may not use this reference to change the object it referes to. For example if you have two objects of type Counter

Counter obj1, obj2;

then you may not write

++obj1 = obj2;

because due to the qualifier const the object refered to by the reference returned by operator ++ is immutable.

If you would remove the const qualifier in the operator declaration then this statement

++obj1 = obj2;

would be valid.

In fact it is not a good idea to declare the preincrement operator returning const reference. Usually it declared without the const qualifier

Counter& opperator++ ();

In this case it behaves the same way as the preincrement operator ++ for arithmetic tyoes. For example this code is valid

int x = 1;

++x = 2;

and the result is x = 2;

To illustrate my comment here an example:

class xy
{
private:
    int i; // const int i when object of xy is const
    int *ptr; // int *const ptr when object of xy is const

public:
    xy() : i(0), ptr(&i) { } // const i initialized to 0, only possible in constructor
                             // ptr pointing to i

    void set (int s) { i=s; } // changes i, not possible to define this member function
                              // as const like read()

    int read () const { return i; } // reads i

    void ptr_set (int s) const { *ptr = s; }
};

int main ()
{
    const xy obj;
    int read = obj.read(); // read() is allowed for const objects

    obj.set(10); // not allowed! you could not define set() as const (like read()):
                 // there would be a compiler error, because this member function
                 // would try to change const i

    obj.ptr_set(10); // allowed, would change i through pointer

    return 0;
}

Btw, can anyone explain why something like obj.ptr_set(10) is after all possible in sense of const correctness?

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