[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


From following code segments if remove (both in declaration...

<< Back to: C++ FAQ (part 1 of 10)

Question by Boominathan
Submitted on 7/2/2003
Related FAQ: C++ FAQ (part 1 of 10)
Rating: Rate this question: Vote
From following code segments
if remove (both in declaration and definition) const(at end) from the memberfunction
bool operator == (const A& a) const;
I get compiler error saying operator was ambiguous

but I remove (both in declaration and definition) const(at end) from the member function
bool operator == (A& a) const;

compiles properly. I could not define the behavior why?  I really appreciate you could define the behavior.




#include <iostream.h>

class A
{
   private:
      int x;
   public:
      bool operator == (A& a)const;
      bool operator == (const A& a);
      void setX(int i) { x = i; }
      int  getX() { return x; }
};


bool A::operator == (const A& a)
{
   cout << "const version called " << endl;
   if(this == &a) return true;
   if(x == a.x) return true;
   return false;
}

bool A::operator == (A& a)const
{
   cout << "Non-const version called " << endl;
   if(this == &a) return true;
   if(x == a.x) return true;
   return false;
}

int main()
{
   A a1;
   const A& a2 =a1;
   A a3;

   a1.setX(5);
   a3.setX(7);
   if(a1 == a3)  cout << " Objects are same " << endl;
   else         cout << " Objects are not same " << endl;
   if(a2 == a3)  cout << " Objects are same " << endl;
   else         cout << " Objects are not same " << endl;
   if(a1== a2)  cout << " Objects are same " << endl;
   else         cout << " Objects are not same " << endl;
   if(a3== a2)  cout << " Objects are same " << endl;
   else         cout << " Objects are not same " << endl;
   if(a3== a3)  cout << " Objects are same " << endl;
   else         cout << " Objects are not same " << endl;

   return 0;
}



Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: C++ FAQ (part 1 of 10)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.