No time for friends->no friends

Pages: 12
@enemy

Your posts are confusing, and it doesn't help that you keep double/triple posting and not indicating which posts you are responding to.

enemy wrote:
"Does nothing" or "assings value 0 to b.i"<- that is first question.//Trying to find out what caused such a confusion.

^ In the wikipedia example, you really do assign value 0 to b.i. But the overall effect is nothing because b only exists in the scope of the constructor.

enemy wrote:
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?

^ The b in A's constructor is a copy of whatever class B object was passed as a parameter (pass by value vs pass by reference). So I guess that you can say the b in question is declared in the constructor function's header. As for the original B object which was passed as a parameter, it exists in whatever function created the A object and called its constructor. As for where the original B object was declared, it could have been anywhere. Who knows?

enemy wrote:
>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;
}

^ As others have already said, the changes are not preserved because b only exists within the scope of the constructor. That's because the class B object was passed by value, not by reference (such as in the example I gave).

I hope that answers your questions.
Last edited on
closed account (SECMoG1T)
Hi , xd i wasn't able to respond , i was away but my friends did to add on what they said , i will give you some simple illustration;

. classes and structs
- one of the reasons we use these constructs is to group data together.
- classes do have data members{variables} and procedures for manipulating data
stored within the classes

2. friendship
- according to the natural rules of encapsulation in c++ a class best enforces data encapsulation by keeping all data members private and only allowing access to them via procedures { functions}.
** when it comes to friendship , we want to violate this encapsulation rule for access advantage to the class data, here we allow other classes and procedure to directly access the class *data* without using procedures{getters&setters} but all other class properties remain the same;

a. data initialization, manipulation etc. aren't affected by friendship, only the means of
access is.

here is an example similar to what my friends gave:

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
class foo
{
  private:
     int value;
     string name;
  public:
     friend ostream& operator<<(ostream& os, const foo& obj);//this operator is reasonably
                                                                     // friend to minimize operations required.
     foo(int val,string s):value(val),name(s){}
};

ostream& operator<<(ostream& os,foo& obj)
 {
    os<<obj.value<<" "<<obj.name<<endl;//direct access to members is legal here
    return os;
 }


///compare to what could be done without using friendship

class bar
{
  private:
    int value;
    string name;
  public:
    int get_value() const;///we would have to use getters
    string get_name() const;// hence above we used friendship to cut this access cost
                                           //but that blows a hole into data encapsulation.
};

  ostream& operator<<(ostream& os,const bar& obj);//not a friend hence declared outside the class

int bar::get_value() const{return value;}
string bar::get_name() const{ return name;}/// this is the legal access means when encapsulation is maintained.

ostream& operator<<(ostream& os,const bar& obj)
{ 
 ///os<<obj.value<<" "<<obj.name<<endl; this access method would be illegal here 
 /// because all members are private.
   os<<obj.get_value()<<" "obj.get_name()<<endl;///this is what encapsulation allows at
                                             //best and above we did circumvent that by making operator<< a friend to the class foo.
   return os;
 }


/// both classes have overloaded versions of << operator which basically achieves the same thing but uses different access modes to class data members. 


Well that's what friendship entails.

i hope that would help too.
Last edited on
Topic archived. No new replies allowed.
Pages: 12