i declared 2 class, and with a function from one class i want to access the data in the other class but im not sure how this can be done. when i tried, i kept on getting error messages.
i declared 2 class, and with a function from one class i want to access the data in the other class but im not sure how this can be done. when i tried, i kept on getting error messages.
You can make the class you need to access a friend of the class that is doing the accessing.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
can u give an example code?
>can u give an example code?
Code:class myclass { friend class some_class; ... }
My best code is written with the delete key.
The following code allows a member function from one class to access the other class
if you want all the member functions of one class to access the other then include friend friendclass; in the Class definition.Code:#include <friendclass.h> Class Myclass { public: private: friend friendclass::friendfunction(Myclass Aclass); };
"Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
-- Cowper
Operating Systems=Slackware Linux 9.1,Windows 98/Xp
Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)
You may teach a person from now until doom's day, but that person will only know what he learns himself.
Now I know what doesn't work.
A problem is understood by solving it, not by pondering it.
For a bit of humor check out xkcd web comic http://xkcd.com/235/
Does this allow all methods of some_class access all members of myclass, even private members of someclass?Code:class myclass { friend class some_class; ...
er.... this code dont work
i get an message keep saying that Undefined SYmbol "access"Code:#include <iostream.h> class myclass { friend class yourclass; protected: int variable; public: myclass(); }; myclass :: myclass() { variable = 1; } class yourclass { public: yourclass(); }; yourclass :: yourclass() { cout << access.variable; } int main() { myclass access; yourclass access2; return 0; }
if i take off access and just leave variable, it says undefined symbol "variable"
Last edited by Chobo_C++; 03-16-2004 at 05:48 PM.
That's because yourclass has no idea what myclass variables you've declared in main.
Code:#include <iostream> using namespace std; class myclass { friend class yourclass; protected: int variable; public: myclass(); }; myclass :: myclass() { variable = 1; } class yourclass { public: yourclass ( myclass& ); }; yourclass :: yourclass ( myclass& access ) { cout << access.variable; } int main() { myclass access; yourclass access2 ( access ); return 0; }
My best code is written with the delete key.
originally posted by elad
If you make the entire class a friend then all the member functions have access to all the data including private members yes. A friend function is the same as an ordinary function except that it can access all the members of a class without restriction.Does this allow all methods of some_class access all members of myclass, even private members of someclass?
Last edited by manofsteel972; 03-17-2004 at 11:52 PM.
"Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
-- Cowper
Operating Systems=Slackware Linux 9.1,Windows 98/Xp
Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)
You may teach a person from now until doom's day, but that person will only know what he learns himself.
Now I know what doesn't work.
A problem is understood by solving it, not by pondering it.
For a bit of humor check out xkcd web comic http://xkcd.com/235/