Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
2 captures
09 Feb 2010 - 06 Mar 2010
FebMARApr
Previous capture06Next capture
200920102011
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:alexa_web_2010
this data is currently not publicly accessible.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20100306184223/http://www.codeproject.com:80/KB/cpp/cppoperproblem.aspx
Click here to Skip to main content
EmailPassword helpLost your password?

Introduction

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

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 themfriends 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 werefriends, 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 bothfriends, and access the private data through a reference specified as an argument).

The solution

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.

History

This article was submitted on April 09, 2009.

 FirstPrevNext
Generalthere is no such problem with vc++6.0
wanft
4:14 9 Apr '09  
GeneralRe: there is no such problem with vc++6.0
Badea Florin
10:11 9 Apr '09  
Last Visit: 8:42 6 Mar '10     Last Update: 8:42 6 Mar '101


Last Updated 9 Apr 2009 |Advertise |Privacy |Terms of Use |Copyright ©CodeProject, 1999-2010

[8]ページ先頭

©2009-2025 Movatter.jp