|
-
August 20th, 2007, 04:58 AM
#1
Friend function
Hello,
Consider this scenario,
Code:
class B;
class A
{
private:
int x, y;
public:
A(){}
friend void func( A a, B b );
};
class B
{
private:
int i, j;
public:
B(){}
friend void func( A a, B b );
};
Friend function is used to allow non-members to access private data defined inside a class. Now my question is I have two variables x and y in class A, and two variables i and j in class B. Is it possible that I can allow my users (outside class) to access only variable x and not y defined in class A ? Similarly, allow access outside class only to variable i and not to j in class B.
NOTE: The above scenario should happen only when friend is defined.
Thanks
Last edited by Jacko123; August 20th, 2007 at 05:01 AM.
-
August 20th, 2007, 05:20 AM
#2
Re: Friend function
No, friend declaration makes both private and protected members accessible.
-
August 20th, 2007, 05:38 AM
#3
Re: Friend function
Directly it's not possible but you might put those variables which you don't want friend function to access in separate base classes holding that variable. Not sure on this. Maybe someone will correct/refine this.
Appreciate others by rating good posts
"Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett
-
August 20th, 2007, 05:49 AM
#4
Re: Friend function
At the first seconds I had a similar thought but since I guess that inherited class need access in some way to base class members the friend function will also have access. Maybe there is a scenario where the method can be used however. Can't think of any at the moment so I join you sunnypalsingh in the 'not sure crowd'...
-
August 20th, 2007, 06:33 AM
#5
Re: Friend function
Here is an example, how it could be done (even though this is a bit clumsy):
Code:
class A
{
private:
int x, y;
public:
A(){}
class XAccessor {
XAccessor( A& a ) : x( a.x ) {}
friend void func( A& a );
private:
int& x;
};
};
void func( A& a ) {
A::XAccessor(a).x = 0;
a.y = 0; // not allowed
A::XAccessor(a).y = 0; // not possible
}
void otherfunc( A& a ) {
A::XAccessor(a).x = 1; // not allowed
}
Last edited by treuss; August 20th, 2007 at 06:39 AM.
-
August 20th, 2007, 07:17 AM
#6
Re: Friend function
Another way is to make y more private thus:
Code:
class B;
class A
{
private:
int x;
class very_private
{
int y;
friend class A;
} y_access;
friend void func( A a, B b );
};
func can access a.x but cannot access a.y_access.y because friendship isn't transitive.
-
August 20th, 2007, 08:18 AM
#7
Re: Friend function
 Originally Posted by NMTop40
Another way is to make y more private thus:
[code]
class very_private
{
int y;
friend class A;
} y_access;
sorry for interrupting but
y_access followed by class very_private braces then ";"
what kind of a syntax is this? means
am i wrong ? or you ?
Last edited by daemonakadevil; August 20th, 2007 at 08:21 AM.
-
August 20th, 2007, 08:24 AM
#8
Re: Friend function
 Originally Posted by daemonakadevil
sorry for interrupting but
y_access followed by class very_private braces then ";"
what kind of a syntax is this? means
am i wrong ? or you ?
Brother he is creating object of class very_private nothing more then that.
-
August 20th, 2007, 08:26 AM
#9
Re: Friend function
Another solution, not best but ya one of the possible solution
Code:
#include <iostream>
class B;
class tempA
{
private:
int x;
public:
tempA(){x=0;}
};
class A : private tempA
{
private:
int y;
public:
A(){y=0;}
friend void func( A a, B b );
};
class tempB
{
private:
int i;
public:
tempB(){i=0;}
};
class B:private tempB
{
private:
int j;
public:
B(){j=0;}
friend void func( A a, B b );
};
void func(A a, B b)
{
std::cout<<"A.y = "<<a.y<<" , B.j = "<<b.j<<std::endl;
//std::cout<<"A.x = "<<a.x<<" , B.j = "<<b.i<<std::endl;
// can't use this statement because now x and i are private members
}
int main()
{
A a;
B b;
func(a,b);
return 0;
}
-
August 20th, 2007, 09:37 AM
#10
Re: Friend function
The problem with your solution is that x is private to A as well, and i is private to B, i.e. A cannot access x and B cannot access i.
-
August 20th, 2007, 10:28 AM
#11
Re: Friend function
 Originally Posted by NMTop40
The problem with your solution is that x is private to A as well, and i is private to B, i.e. A cannot access x and B cannot access i.
Yes you correctly identified problem in my code. Thanks for that.
I have to use tempA as friend class in class A. and same for tempB also
Thanks for correcting my code.
-
August 21st, 2007, 03:22 AM
#12
Re: Friend function
Nice solutions there NMTop40 and treuss! But what kind of a requirement is this? Sounds like a flawed design.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 21st, 2007, 04:32 AM
#13
Re: Friend function
 Originally Posted by exterminator
Sounds like a flawed design.
Doesn't that apply to any use of friend?
-
August 21st, 2007, 04:44 AM
#14
Re: Friend function
Doesn't that apply to any use of friend?
Not necessarily. For example, overloading operator<< and operator>> for I/O streams is valid use of friend functions. Consider the arguments given by Does "friend" violate encapsulation?
-
August 21st, 2007, 05:42 AM
#15
Re: Friend function
 Originally Posted by treuss
Doesn't that apply to any use of friend?
How? Ask a question, why is that the friendship in the OP's case is causing you to "split" your original class into an internal class with an explicit accessor or causing you to create extra types (that might not sometimes make sense as individual entities or abstractions) and "different" friendships? Does it really make sense to use friends when the friend should have access to just part of the private data and not all of those? Or better put it that you want to ensure/enforce that access to other private data of a friend needs to be protected? Why need to make such a distinction?
To me it looks like:- original friendship does not make sense
- the original class needs to be segregated into more granular distict types (in which case, your or NMTop40's solutions fit right)
- the point to protect rest of the data is immaterial
A class can have many members and considering operator>> overloads which can be friend, do you protect the data members that you don't want to publish via the operator to the streams?
Re-reading the original post,
 Originally Posted by Jacko123
Is it possible that I can allow my users (outside class) to access only variable x and not y defined in class A ? Similarly, allow access outside class only to variable i and not to j in class B.
I am guessing that it doesn't even concern a friend. The question is about users of the class and not friends. So, since the data members in A and B are private, nothing is accessible. For a few selected data members to be accessible the only thing needed is a modifier and an accessor (getter/setter). May be the orginal post is not clear on this point but my above arguments hold.
Last edited by exterminator; August 21st, 2007 at 05:48 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|