No time for friends->no friends

Pages: 12
Hello!
Is that with friends really more complicated then it seems?

Here is the original wikipedia example, what did they want to say with:"

"the object has to be passed as a parameter to the function"


I just TRY to FINISH this code using main...what is that I do not see?

Many thanks for help, plese do not get furious!!! ( cause I am the one who already did and think it is enough!!!)

P.s. Just "mutating" the code, but still believe some of you will be faster...

MANY THANKS!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
class B {
    friend class A; // A is a friend of B
 
private:
    int i;
};
 
class A {
public: 
    A(B b) {//Note that the object has to be passed as a parameter to the function
        b.i = 0; // legal access due to friendship
    }
};
"An object has to be passed as a parameter to the function".

OK, "object" has been passed. But, where and how has it been created? We just had world war trying to do anything sensible from that frame. That function a seems to serve as a kind of setValue function, doesn't it?


OK, now I want the same SIMPLE thing: to make main (or any!) function just to cout it.
Unbelievable how that can be a problem!
OK, now I want the same SIMPLE thing: to make main (or any!) function just to cout it.
Unbelievable how that can be a problem!

If you want to have any function be able to access B::i then you should just make it public instead of private...
Last edited on
Look, here I just "tested" wikipedia's example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class B {
    friend class A; // A is a friend of B
 
private:
    int i;
};
 
class A {
public: 
    A(B b) {//Note that the object has to be passed as a parameter to the function
        b.i = 0; // legal access due to friendship
    }
};

int main(){}


complier says:
No errors or program output.

OK, so the code is valuable itself.
(It means, nothing is missing!).

But, this valuable code creates one object b, whose index )i= is 0.

What in the world I have to do- wiht MINIMAL changes to THIS code, to get output 0, how to write and present it -> from THIS particualr code, not the similar one!


MANY THANKS!!!
How and where to PRESERVE this invisible B b whose value is 0?

Is it simply IMPOSSIBLE to see it couted without drastic changes?

We got it in last solution, but in that solution no object has been passed to the function a. And as they INSIST , OBVIOUSLY , that the OBJECT has to be passed..THAT makes a confusion, how to print it then that way..

How and where to PRESERVE this invisible B b whose value is 0?

Is it simply IMPOSSIBLE to see it couted without drastic changes?

We got it in last solution, but in that solution no object has been passed to the function a. And as they INSIST , OBVIOUSLY , that the OBJECT has to be passed..THAT makes a confusion, how to print it then that way..


I am very confused as to what you are trying to accomplish here. Are you trying to print a 0? Are you trying to copy a B class around? Are you trying to create a B in main and change its values?
1
2
3
4
5
6
7
8
9
10
11
class B
{
public:
    int i; // i is public, any function can access it
};

int main()
{
    B b;
    b.i = 0;  // works because B::i is public
}

If that is not what you meant, then maybe you meant this:

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
class B
{
    friend class A;
private:
    int i;  // i is private, only A and B classes can access it
};

class A
{
public:
    void set(B& b, int x)
    {
        b.i = x;
    }
    int get(const B &b)
    {
        return b.i;
    }
};

int main()
{
    A a;
    B b1, b2;
    a.set(b1, 1);
    a.set(b2, 2);
    std::cout << a.get(b1) << ' ' << a.get(b2);
}


Sorry, I can't understand exactly what you're trying to ask.
Hello, fg109!

Yes, that is the kind of output I wanted to get, but question is very simple:

we have this code, which obvioulsy works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class B {
    friend class A; // A is a friend of B
 
private:
    int i;
};
 
class A {
public: 
    A(B b) {//Note that the object has to be passed as a parameter to the function
        b.i = 0; // legal access due to friendship
    }
};

int main(){}


So, if it works, it means, THERE exists one B b object, whose b.i=0.
We only can't see it , right?

OK, what I want is that MAIN writes it out, without adding additional functions (or adding minimal functions, if no other answer possible).

So, how to get output from this code with MINIMAL ADDING anithing?


Many thanks!!!
OK, data member is in one class (B), and constructor in another class (A)!!!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class B {
    friend class A; // A is a friend of B
 
private:
    int i;
};
 
class A {
public: 
   A(B b) {//Note that the object has to be passed as a parameter to the function
        b.i = 0; // legal access due to friendship
    }
};

int main(){

cout<<b.i<<endl; //Line 17: error: 'b' was not declared in this scope
}


Question: how to declare it in the mian sope, simplest way( is there any way SHORTER and more simple then examples I got as answers before?)


MANY THANKS!!

(In fg109's example we have changed one function (constructor) from class A to two functions. Is there a simpler solution, or is that the ONLY possible solution?)

Last edited on
we have this code, which obvioulsy works:

Well, let me write an another program that performs the exact same operations as your program does:
1
2
3
int main()
{
}


In other words: your program does not create any objects of type A or type B. No member functions of those types are called.

how to declare it in the mian sope
1
2
3
4
int main() {
  B bar;
  A foo( bar );
}

This, however, does not do what you want, because the by value argument b to the constructor of A has the lifetime of the body of the constructor. Changes to b are not preserved anywhere.

Overall, one should avoid friends. Classes are to promote and enforce encapsulation and friendship blows holes to the fine walls.
Last edited on
closed account (SECMoG1T)
Yea i guess what confused the op was what @keskiverto explained
because the by value argument b to the constructor of A has the lifetime of the body of the constructor might be he/she was wondering about where the value b.i=0 was stored , if that was it , the b arg was copied ,assigned and then destroyed as the constructor exited like #keskiverto said so that b exists no more.
Hello!
Yes ,exactly that! I thought about sth like dynamic memory for objects like b.i???
closed account (SECMoG1T)
well the constructor in your example simply does nothing , passing the parameter was only meant for learning, to preserve the value maybe you'd need a "B" data member in that class then initialize it to the value of the b:}
"Does nothing" or "assings value 0 to b.i"<- that is first question.//Trying to find out what caused such a confusion.
Lets imagie it does ,and I just want to save it so that it survives the "scope time", probably using:

http://www.cs.fsu.edu/~jestes/cop3330/notes/dma.html

Now, the question is if I have at all sucha B b object that I can preserve at all, and, if so, then where/how
Class A takes (B b) as function argument. Ok. But where has that object been declared? Is that a question of syntax, or is the example simply TOO SIMPLIFIED?
Changes to b are not preserved anywhere.


Can they be at all?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class B {
    friend class A; // A is a friend of B
 
private:
    int i;
};
 
class A {
public: 
    A(B b) {//Note that the object has to be passed as a parameter to the function
        b.i = 0; // legal access due to friendship
    }
   dm=new b;  //Line 13: error: expected type-specifier before 'b'
};

int main(){
cout<<b.i<<endl;

delete b;

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class B {
    friend class A; // A is a friend of B
 
private:
    int i;
};
 
class A {
public: 
    A(B b) {//Note that the object has to be passed as a parameter to the function
        b.i = 0; // legal access due to friendship
    }
   dm=new B b; //Line 13: error: `new' cannot appear in a constant-expression
};

int main(){
cout<<b.i<<endl;

delete B b;

return 0;
}
closed account (z05DSL3A)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>

using namespace std;

class B 
{
	friend class A; 
public:
	B():i(99){}

	void Print()
	{
		cout << i << endl;
	}
private:
	int i;
};

class A 
{	
public:
	A()
	:theB()
	{
		theB.i = 10;
	}

	A(B in)
	:theB(in)
	{
		theB.i = 0;
	}

	void Print_B()
	{
		theB.Print();
	}
private:
	B theB;
};


int main()
{
	
	A anA;

	anA.Print_B();

	B aB;
	aB.Print();
	
	A anotherA(aB);

	anotherA.Print_B();
	aB.Print();

	return 0;
}
Pages: 12