![](wink.gif)
friend functions in MS Visual C++?
Hey guys,
I''m doing a lab for my CS class, I''m using MS Visual C++ 6.0 on Windows 2000, and when i have a friend function, the compiler can''t seem to let the friend function access the private data members of the class that the friend function prototype is in...
Anything i can do?
Thanks guys
-Spiky
I want to work for Squaresoft
![](wink.gif)
I want to work for Squaresoft ;)
I just compiled this code with VC++ 6, and everything worked perfectly. You've probably made a mistake in the prototype, so here's my code for comparison:
It outputs "1337", just like it should. I hope this helps.
Edited by - jonnyfish on February 10, 2001 11:27:40 AM
#include <iostream>using namespace std;class CPrivate{ friend class CFriend; friend void Print(const CPrivate& p);public: CPrivate():m_iPrivate(0) {} virtual ~CPrivate() {}private: int m_iPrivate;};class CFriend{public: CFriend() {} CFriend(CPrivate& p) { p.m_iPrivate = 1337; } virtual ~CFriend() {}};void Print(const CPrivate& p){ cout << p.m_iPrivate << endl; return;}int main(int argc, char* argv[]){ CPrivate p; CFriend f(p); Print(p); return 0;}
It outputs "1337", just like it should. I hope this helps.
Edited by - jonnyfish on February 10, 2001 11:27:40 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement