const before parameter vs const after function name c++

C++FunctionConstants

C++ Problem Overview


What is the difference betweeen something like this

friend Circle copy(const Circle &);

and something like this

friend Circle copy(Circle&) const;

I know const after the function is used to tell the compiler that this function won't attempt to change the object it is called on, what about the other one?

C++ Solutions


Solution 1 - 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;
};

Solution 2 - C++

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).

Solution 3 - C++


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
                       }
           }; 

Solution 4 - C++

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.

Solution 5 - C++

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.

Solution 6 - C++

friend Circle copy(const Circle &);//refers to constant parameter of the function. cant' change the value stored by parameter.

Need to remove friend in your example Circle copy(Circle&) const; //can't change this poniter value named as Constant member function

Solution 7 - C++

friend Circle copy(const Circle &);

The value of parameter will not be changed during the function calls.

friend Circle copy(const Circle &)const ; 

The function is an accessor that does not change any value of class members. Generally, there are to types of functions: accessors and mutators. Accessor: examines but does not change the state of its object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjazzybazzView Question on Stackoverflow
Solution 1 - C++Andy ProwlView Answer on Stackoverflow
Solution 2 - C++John ZwinckView Answer on Stackoverflow
Solution 3 - C++Ramesh ChoudharyView Answer on Stackoverflow
Solution 4 - C++WaqarView Answer on Stackoverflow
Solution 5 - C++Alex ChamberlainView Answer on Stackoverflow
Solution 6 - C++shivakumarView Answer on Stackoverflow
Solution 7 - C++SamView Answer on Stackoverflow