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 {
friendclass 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!
class B {
friendclass 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!
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?
class B
{
friendclass 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.
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 {
friendclass 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?
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 {
friendclass 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?)
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.
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.
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:}
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?
class B {
friendclass 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;
}
class B {
friendclass 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;
}