top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

0 votes

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 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 by Siddhi Patel
Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.