friend classes_ why constructor shouldnot be private?

Hello!
Please, classes A and B are friends!
Please, where is the mistake, why is another class not seing elements from class b, if it is its friend? A should see b. Many thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  class B {
    friend class A; // A sees into the B class
 
private:
    int i;
  B(int c): i(c) {}
};
 

 

class A {
public: 
   void a(B b);
};

void A::a(B b){
b.i=10;
}


int main(){
B obj1(2);
B obj2(3);
B obj3(4);


int z;
z=obj1.i;
cout<<z<<endl;
return 0;
}
B's constructor is private. so line 23 to 25 are "inaccessible"
similarly you can't access obj1.i
Hello!
Please, what is the way to solve it? (except turning B constructor to public?

The point of the example is to show what friend class sees, if I turn B constructor to public, it is not a good example to show what friend classes do!


Have no inspiration,many thanks!!!
Last edited on
if B's constructor is private, you cant create B objects in main function.
create where you can.(inside A)
Hello! Thanks for making it clear!
Please, what is missing in that code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class B {
    friend class A; // A sees into the B class
 
private:
    int i;
 
};
 

 

class A {
public: 
   A(int);
   void a(B b);
};


A::A(int c):i(c){};

void A::a(B b){
b.i=10;
}


int main(){
B obj1(2);
B obj2(3);
B obj3(4);


int z;
z=obj1.i;
cout<<z<<endl;
re


Compiler says: Line 19: error: class 'A' does not have any field named 'i'


(A really does not, but B does and it is friend to B and can see into it, can't it???)

(Do I probably have to give it a virtual object as parameter too and how to sintax it???)
Last edited on
> Compiler says: Line 19: error: class 'A' does not have any field named 'i'

yes. the thing here is: what is "i" ? its not a member of A. but from A you can access it through a B object.
Hello!
Yes ,Iknow it all, but the code I wrote does not work, so I wonder how to "fix" the code.
I moved the constructor from B to A, there is obvioulsy sth missing...sth that will "tell" A that it "can" see into B, and this "something" is sth MORE then just declaring A as B's friend in the line2...
i see you removed B's constructor, which was private. yet you don't have any setValue function. so B's private variable i is completely unusable!
Hello!
I am really confused, sorry if I am too boring!!!
Why do I need setValue if I have an constructor?

U want to say, sth to initialise values (function or constructor) HAS TO BE in the same class, even if anohter one sees inside???

I tried to "fix" ,still the same error ("I" is still unvisible..)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class B {
    friend class A; // A sees into the B class
 
private:
    int i;
 
};
 

 

class A {
public: 
   A(int);
   void a(A b);
};


A::A(int c):i(c){};        //.cpp: In constructor 'A::A(int)':
                                       //Line 19: error: class 'A' does not have any field named 'i'

void A::a(A b){
b.i=10;
}


int main(){
A obj1(2);
A obj2(3);
A obj3(4);


int z;
z=obj1.i;
cout<<z<<endl;
return 0;
}
Last edited on
Okey okey, calm down, back to the beginning.

Everything in your first example is fine, there is only 1 Problem.

The constructor B(int) is private.
Because of that, you can't create B-Objects in main(), you can only create them in A-Objects.

Here is a working example, I just put the constructor of B inside of A and printed the data there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

class B
{
    friend class A;
   
private:
    B(int c) : i(c) {}
    int i;
};

class A
{
public:    
    void a()
    {
        B b(10);
        std::cout << b.i << std::endl;
    }
};

int main()
{
    A a;
    a.a();
}
Last edited on
UUUUUUUUUUUUUUUUUUUUUUUUps!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

MANY THANKS!!!!

Thinking what was happening here now:

so: the constructor is actually still in B, but we just create B objects in A.

Brain apear-tearing at the very moment!!!

MANY THANKS!!!
:'DD

You're welcome ;)
Topic archived. No new replies allowed.