top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: What is the difference between reference and constant reference ?

0 votes
268 views

I have seen at many places that object is passed as reference and as constant reference.
I want to know the usage of each one.

posted May 31, 2019 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The difference between a reference and a const reference is merely that you can modify the referenced value (or call non-const member functions on it) when you have a plain reference, but can only call const member functions on an object for which you have a const reference, and you cannot modify it if it is a built-in type.
for example..

class A
{
  int buf[1000];
  public: A& fun() 
  {
   buf[0]++; return *this;
  }
  const A& fun2() 
  {
   buf[0]++; return *this;
   }
};
answer Jun 4, 2019 by Siddhi Patel
...