473,396 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why friendship not applicable to function arguments

When compiling using gcc 2.95.3, I get the following error for the
code snippet posted below.
bash-2.03$ g++ membAccess.C
membAccess.C: In function `int main()':
membAccess.C:13: `struct A B::m_a' is private
membAccess.C:38: within this context
Is this how it is supposed to work or is this a compiler specific issue

1 #include <iostream>
2
3
4 struct A
5 {
6 int y;
7 };
8
9 void useAalter (A* pA);
10 class B
11 {
12
13 A m_a;
14 public:
15 B (A& a)
16 {
17 m_a = a;
18 }
19 A* getA () { return &m_a;}
20 friend void useAalter(A*pA);
21 };
22
23 void useA (A* pA)
24 {
25 std::cout << pA->y << std::endl;
26 }
27
28 void useAalter (A* pA)
29 {
30 std::cout << pA->y << std::endl;
31 }
32
33 int main ()
34 {
35 A a = {45};
36 B bInst (a);
37 useA (bInst.getA());
38 useAalter (&(bInst.m_a)) ;
39 }

Jun 29 '06 #1
4 1414

"Ninan" <ni****@yahoo.com> wrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
When compiling using gcc 2.95.3, I get the following error for the
code snippet posted below.
bash-2.03$ g++ membAccess.C
membAccess.C: In function `int main()':
membAccess.C:13: `struct A B::m_a' is private
membAccess.C:38: within this context
Is this how it is supposed to work or is this a compiler specific issue

1 #include <iostream>
2
3
4 struct A
5 {
6 int y;
7 };
8
9 void useAalter (A* pA);
10 class B
11 {
12
13 A m_a;
14 public:
15 B (A& a)
16 {
17 m_a = a;
18 }
19 A* getA () { return &m_a;}
20 friend void useAalter(A*pA);
This makes the function useAalter a friend. So far, so good.
21 };
22
23 void useA (A* pA)
24 {
25 std::cout << pA->y << std::endl;
26 }
27
28 void useAalter (A* pA)
29 {
30 std::cout << pA->y << std::endl;
31 }
32
33 int main ()
34 {
35 A a = {45};
36 B bInst (a);
37 useA (bInst.getA());
38 useAalter (&(bInst.m_a)) ;


But here, you're getting an error because you're trying to resolve
&(bInst.m_a), and pass that as an A*. But you're in main() here, not in
useAalter() yet. The value you're trying to pass has to be resolved _here_,
then passed as an A* parameter to useAalter(). And it can't be resolved
here, because main() is not a friend of the class A.

If you want to access the private member of bInst, you could make the
parameter to useAalter() some kind of reference or pointer to a B instance,
instead of a pointer to an A instance. Then you'd pass bInst (or its
address) to useAalter(), instead of the address of bInst.m_a.

-Howard
Jun 29 '06 #2

Ninan wrote:
When compiling using gcc 2.95.3, I get the following error for the
code snippet posted below.
bash-2.03$ g++ membAccess.C
membAccess.C: In function `int main()':
membAccess.C:13: `struct A B::m_a' is private
membAccess.C:38: within this context
Is this how it is supposed to work or is this a compiler specific issue

1 #include <iostream>
2
3
4 struct A
5 {
6 int y;
7 };
8
9 void useAalter (A* pA);
10 class B
11 {
12
13 A m_a;
14 public:
15 B (A& a)
16 {
17 m_a = a;
18 }
19 A* getA () { return &m_a;}
20 friend void useAalter(A*pA);
21 };
Because useAalter is a friend of class B, it is allowed to access
private members in that class.

28 void useAalter (A* pA)
29 {
30 std::cout << pA->y << std::endl;
31 }
But useAalter does not actually access anything in class B. This
function makes no use of its frienship to B whatsoever. It accesses the
identifier A::y. This is allowed because y is public in struct A.
32
33 int main ()
34 {
35 A a = {45};
36 B bInst (a);
37 useA (bInst.getA());
38 useAalter (&(bInst.m_a)) ;
39 }


Here, it is the function main which accesse the name B::m_a in line 38.
Since main isn't a friend of the class B and m_a is private, this
doesn't work.

Access protection is concerned with name lookups, not pointer
dereferences.

The function useAalter has no idea that the A * which it is working
with is in fact B::m_a. It could be any A struct whatsoever:

A a = { 45 };

useAalter(&a); // nothing to do with any B object

Jun 29 '06 #3

Howard wrote:
"Ninan" <ni****@yahoo.com> wrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
When compiling using gcc 2.95.3, I get the following error for the
code snippet posted below.
bash-2.03$ g++ membAccess.C
membAccess.C: In function `int main()':
membAccess.C:13: `struct A B::m_a' is private
membAccess.C:38: within this context
Is this how it is supposed to work or is this a compiler specific issue

1 #include <iostream>
2
3
4 struct A
5 {
6 int y;
7 };
8
9 void useAalter (A* pA);
10 class B
11 {
12
13 A m_a;
14 public:
15 B (A& a)
16 {
17 m_a = a;
18 }
19 A* getA () { return &m_a;}
20 friend void useAalter(A*pA);
This makes the function useAalter a friend. So far, so good.
21 };
22
23 void useA (A* pA)
24 {
25 std::cout << pA->y << std::endl;
26 }
27
28 void useAalter (A* pA)
29 {
30 std::cout << pA->y << std::endl;
31 }
32
33 int main ()
34 {
35 A a = {45};
36 B bInst (a);
37 useA (bInst.getA());
38 useAalter (&(bInst.m_a)) ;


But here, you're getting an error because you're trying to resolve
&(bInst.m_a), and pass that as an A*. But you're in main() here, not in
useAalter() yet. The value you're trying to pass has to be resolved _here_,
then passed as an A* parameter to useAalter(). And it can't be resolved
here, because main() is not a friend of the class A.


The error message "`struct A B::m_a' is private" is saying that main is
not a friend of class B and thus class B's private member could not be
accessed in main.

If you want to access the private member of bInst, you could make the
parameter to useAalter() some kind of reference or pointer to a B instance,
instead of a pointer to an A instance. Then you'd pass bInst (or its
address) to useAalter(), instead of the address of bInst.m_a.

-Howard


Jun 29 '06 #4

"Yong Hu" <yh*******@gmail.com> wrote in message
news:11**********************@75g2000cwc.googlegro ups.com...

Howard wrote:
"Ninan" <ni****@yahoo.com> wrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
> When compiling using gcc 2.95.3, I get the following error for the
> code snippet posted below.
> bash-2.03$ g++ membAccess.C
> membAccess.C: In function `int main()':
> membAccess.C:13: `struct A B::m_a' is private
> membAccess.C:38: within this context
> Is this how it is supposed to work or is this a compiler specific issue
>
> 1 #include <iostream>
> 2
> 3
> 4 struct A
> 5 {
> 6 int y;
> 7 };
> 8
> 9 void useAalter (A* pA);
> 10 class B
> 11 {
> 12
> 13 A m_a;
> 14 public:
> 15 B (A& a)
> 16 {
> 17 m_a = a;
> 18 }
> 19 A* getA () { return &m_a;}
> 20 friend void useAalter(A*pA);


This makes the function useAalter a friend. So far, so good.
> 21 };
> 22
> 23 void useA (A* pA)
> 24 {
> 25 std::cout << pA->y << std::endl;
> 26 }
> 27
> 28 void useAalter (A* pA)
> 29 {
> 30 std::cout << pA->y << std::endl;
> 31 }
> 32
> 33 int main ()
> 34 {
> 35 A a = {45};
> 36 B bInst (a);
> 37 useA (bInst.getA());
> 38 useAalter (&(bInst.m_a)) ;


But here, you're getting an error because you're trying to resolve
&(bInst.m_a), and pass that as an A*. But you're in main() here, not in
useAalter() yet. The value you're trying to pass has to be resolved
_here_,
then passed as an A* parameter to useAalter(). And it can't be resolved
here, because main() is not a friend of the class A.


The error message "`struct A B::m_a' is private" is saying that main is
not a friend of class B and thus class B's private member could not be
accessed in main.


Yeah, that's what I meant. I accidently said "not a friend of class _A_",
when I mean "_B_".

-Howard


Jun 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
4
by: puzzlecracker | last post by:
I have encountered certain confusion while scrutinizing the long distance friendship engendering violation of encapsulation. If object (be it a class, variable, function, etc) has an internal...
7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
by: tom.s.smith | last post by:
I know that friendship isn't inherited in C++, and have two questions. (1) Why? Is it a technical problem, or just by design? If the latter, isn't this a little prescriptive, and shouldn't there...
2
by: Mark P | last post by:
Is there a way to do anything like the following: class A { friend void B::foo(); ... }; class B
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
6
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.