of that class too?
ie,
class afriend
{
friend void alterdata();
private:
int data;
};
so how do i fix this?
void alterdata()
{
data = 5; // error
}
thanks!
of that class too?
ie,
class afriend
{
friend void alterdata();
private:
int data;
};
so how do i fix this?
void alterdata()
{
data = 5; // error
}
thanks!
Straight from Sams in 21 days.....
"If you want to expose your private member data or functions to another class, you must declare that class to a be a friend. This extends the interface of your class to include the friend class."
Remember just because you declare a class to be a member of another doesn't mean that class can access the other classe's method.
For example, I declare you to be my friend, I decide to tell you might secret, that's my business I don't have to have access to your secrets.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
um, er., i think that's not the answer to my question?
my example, alterdata() which is a friend function, and upon the implementation of it, the compiler will output an error, so how do i fix this?
there's only one class here...,
Remember that friend functions are NOT members of the class. That's why the alterdata function definition is not written:Code:class afriend { friend void alterdata(); private: int data; }; void alterdata() { afriend::data = 5; }
afriend::void alterdata()
{
data = 5;
}
But they DO have access to all members of the class. You just have to make sure you qualify those members with the class name.
[EDIT]
Just corrected a small mistake. It's ok now.
[/EDIT]
Last edited by Mario; 05-28-2002 at 10:49 AM.
Regards,
Mario Figueiredo
Using Borland C++ Builder 5
Read the Tao of Programming
This advise was brought to you by the Comitee for a Service Packless World
so a scope resolution does the job, thanks!!! no wait..., i've suddenly thought about this, that doesn't make any sense at all, coz, i mean, it doesn't belong to any object right? so, i'm assigning a value to a data (which is 5) but in what object does data belong??
Considering I wrote that about a week ago, I would say your "suddenly" is way slowOriginally posted by mickey
no wait..., i've suddenly thought about this
But nonetheless you are right, of course. It just happens I only had your example to go through. Still, I went on the err side because i didn't notice the following:
What I wrote can be done when the data member (private or not) was declared as static. These kind of data members exist even before any object gets created.
Otherwise, you should pass the object as an argument of the function.
void alterdata(afriend myfriend)
{
myfriend.data = 5;
}
For this minimalist example you give there is in fact no need to declare the function as friend. You should put it on the public section of your class and it becomes a member function of the class.
Regards,
Mario Figueiredo
Using Borland C++ Builder 5
Read the Tao of Programming
This advise was brought to you by the Comitee for a Service Packless World