I ran into a problem while I was trying to write a small class to test how I could overload operators in C++ in VS6. I will tell you all about it in this small article, and I hope it will help you. I am writing this because in all the C++ books I have read, I haven't come across this problem. I also think this is a problem only for the C++ compiler in VS6. I tried this example in VS2008 and it worked fine.
The problem I am telling you about is aboutfriend
member functions. I tried to overload the>>
,<<,
and==
operators as global operators. And of course, I made themfriend
s of my class. When I compiled the program, I received an error telling me that I can't access the private members of my class from the operator functions. I triple checked that my operator functions werefriend
s, and they were. Here is the initial code:
class Integer{int a;public: Integer(int rv){a=rv;}friendint operator==(const Integer& lv,const Integer& rv);friend ostream&operator<<(ostream& out,const Integer& rv);friend istream&operator>>(istream&in,Integer& rv);};int operator==(const Integer& lv,const Integer& rv){ cout<<"operator==()"<<endl;return lv.a==rv.a;}ostream&operator<<(ostream& out,const Integer& rv){return out<<rv.a;}istream&operator>>(istream&in,Integer& rv){returnin>>rv.a;}
The problem that occurs here is that in the code above, we all assume that thefriend
declarations specify both the declaration of the global function and the fact that this function is afriend
. Apparently, in VC6, this isn't the case with operator functions. If I try to write a normalfriend
function, everything works fine. The code below demonstrates this:
class MyClass{int a;public:friendvoid f(MyClass& mc);};void f(MyClass& mc){ cout<<mc.a<<endl;}
The code above doesn't generate an error even though it is almost the same as the operator definitions (they are bothfriend
s, and access the private data through a reference specified as an argument).
I solved this problem in a very simple manner. I added a declaration for the global operator functions above the class definition code. This seemed to get the job done. Here is the code I added:
class Integer;int operator==(const Integer& lv,const Integer& rv);ostream&operator<<(ostream& out,const Integer& rv);istream&operator>>(istream&in,Integer& rv);
and here is the code for the entire application:
#include"stdafx.h"#include<iostream>usingnamespace std;class Integer;int operator==(const Integer& lv,const Integer& rv);ostream&operator<<(ostream& out,const Integer& rv);istream&operator>>(istream&in,Integer& rv);class Integer{int a;public: Integer(int rv){a=rv;}friendint operator==(const Integer& lv,const Integer& rv);friend ostream&operator<<(ostream& out,const Integer& rv);friend istream&operator>>(istream&in,Integer& rv);};int operator==(const Integer& lv,const Integer& rv){ cout<<"operator==()"<<endl;return lv.a==rv.a;}ostream&operator<<(ostream& out,const Integer& rv){return out<<rv.a;}istream&operator>>(istream&in,Integer& rv){returnin>>rv.a;}int main(int argc,char* argv[]){ Integer i1(8),i2(4);if(i1==i2) { cout<<i1<<", "<<i2<<endl; }else { cin>>i1; } cout<<i1<<", "<<i2<<endl;return0;}
This is it. Please feel free to post your comments and suggestions.
This article was submitted on April 09, 2009.
| |||||||||||
| |||||||||||
|