Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const before parameter vs const after function name c++

The first form means that the (state of the) Circle object bound to the reference which is the parameter of the copy() function will not be altered by copy() through that reference. The reference is a reference to const, so it won't be possible to invoke member functions of Circle through that reference which are not themselves qualified as const.

The second form, on the other hand, is illegal: only member functions can be const-qualified (while what you are declaring there is a global, friend function).

When const qualifies a member function, the qualification refers to the implicit this argument. In other words, that function will not be allowed to alter the state of the object it is invoked on (the object pointed to by the implicit this pointer) - with the exception of mutable objects, but that's another story.

To say it with code:

struct X
{
    void foo() const // <== The implicit "this" pointer is const-qualified!
    {
        _x = 42; // ERROR! The "this" pointer is implicitly const
        _y = 42; // OK (_y is mutable)
    }

    void bar(X& obj) const // <== The implicit "this" pointer is const-qualified!
    {
        obj._x = 42; // OK! obj is a reference to non-const
        _x = 42; // ERROR! The "this" pointer is implicitly const
    }

    void bar(X const& obj) // <== The implicit "this" pointer is NOT const-qualified!
    {
        obj._x = 42; // ERROR! obj is a reference to const
        obj._y = 42; // OK! obj is a reference to const, but _y is mutable
        _x = 42; // OK! The "this" pointer is implicitly non-const
    }

    int _x;
    mutable int _y;
};

C++ class methods have an implicit this parameter which comes before all the explicit ones. So a function declared within a class like this:

class C {
  void f(int x);

You can imagine really looks like this:

  void f(C* this, int x);

Now, if you declare it this way:

  void f(int x) const;

It's as if you wrote this:

  void f(const C* this, int x);

That is, the trailing const makes the this parameter const, meaning that you can invoke the method on const objects of the class type, and that the method cannot modify the object on which it was invoked (at least, not via the normal channels).



LET'S CLEAR ALL CONFUSION RELATED TO const


const came from constant mean something is not changeable but readable.

  1. if we qualify our variable with const keyword ,we can't change it later.
    e.g.
    constint var =25; const variable must be initialized when it's declared.
    var =50; // gives error

  2. if we qualify our pointer variable with const after * then we can't change pointer itself but content of pointer is changeable.
    e.g.
    int *const ptr = new int;
    ptr = new int; //gives error
    // but
    *ptr=5445; //allowed

  3. if we qualify our pointer variable with const before * then we can change pointer itself but content of pointer is not changeable.
    e.g.
    intconst* ptr = new int(85);
    //or
    constint * ptr = new int(85);
    ptr = new int; // allowed
    // but
    *ptr=5445; // gives error

  4. pointer and content both constant
    e.g.
    intconst*constptr = new int(85);
    //or
    constint *constptr = new int(85);
    ptr = new int; // not allowed
    *ptr=5445; // not allowed


  1. Circle copy(const Circle &);
    here const Circle means value of Circle is only readable ,if we try to change value of Circle inside function then it gives error.
  2. friend Circle copy(Circle&) const;
    This type of function is not for non member variable .it is used for class or structure. Here whole function is qualified with const keyword means we can't change object member variable . e.g
    class A{ public :
              int  var;
              void fun1()
                    { var = 50; // allowed
                    } 
              void fun2()const
                       { var=50; //not allowed
                       }
           }; 

Circle copy(Circle&) const;

makes the function const itself. This can only be used for member functions of a class/struct.

Making a member function const means that

  • it cannot call any non-const member functions
  • it cannot change any member variables.
  • it can be called by a const object(const objects can only call const functions). Non-const objects can also call a const function.
  • It must be member function of the class 'Circle'.

Now consider the next one:

Circle copy(const Circle &);

while this one means that the parameter passed cannot be changed within the function. It may or may not be a member function of the class.

NOTE: It is possible to overload a function in such a way to have a const and non-const version of the same function.


One refers to the parameter the other to the function.

Circle copy(const Circle &);

This means that the parameter passed in cannot be changed within the function

Circle copy(Circle&) const;

The const qualified function is used for member functions and means you cannot change the data members of the object itself. The example you posted was nonsensical.

Read right-to-left

If we rewrite the first function as Circle copy(Circle const&);, which means the same thing, it becomes clear that reading right to left becomes useful. copy is a function that takes a const reference to a Circle object and returns a Circle object by reference.