Using friend function, can we overwrite the private member of the class?

Arvind Patel

In the given c++ code the private member of class DEF is getting initialized in the constructor and again inside the friend function. So the redefinition will overwrite the private variables or the value given by the constructor will persist?

#include<iostream>

//class DEF;

class ABC{
        public:
                 int fun(class DEF);
};

class DEF{
        private:
                int a,b,c;

        public:
        DEF():a(1),b(12),c(2){}
        friend  int ABC::fun(class DEF);/*Using friend function to access the private member of other class.*/
        void fun_2();
};

void DEF::fun_2(){
        cout<<"a : "<<&a<<' '<<"b : "<<&b<<' '<<"c: "<<&c<<endl;
        cout<<"a : "<<a<<' '<<"b : "<<b<<' '<<"c: "<<c<<endl;
}

int ABC::fun(class DEF A){
        A.a = 10;
        A.b = 20;
        A.c = 30;

        int data = A.a + A.b + A.c;
        cout<<"a : "<<&(A.a)<<' '<<"b : "<<&(A.b)<<' '<<"c: "<<&(A.c)<<endl;
        cout<<"a : "<<A.a<<' '<<"b : "<<A.b<<' '<<"c: "<<A.c<<endl;
        return data;
}

int main(){
        cout<<"Inside main function"<<endl;
        ABC obj_1;
        DEF obj_2;
        obj_2.fun_2();
        int sum = obj_1.fun(obj_2);
        cout<<"sum : "<<sum<<endl;
        obj_2.fun_2();

}
Mohit Jain

In below line:

int ABC::fun(class DEF A)

You are passing argument by value and thus value of local object is changed.

To ensure the values persist, pass the value by reference as:

int ABC::fun(DEF &A)
//               ^   <-- class is superfluous here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Private function as friend of other class

From Dev

Why can't this public member function call decltype on a private struct member declared inside the class?

From Dev

Friend function of a private inner class

From Dev

friend for private class

From Dev

Friend member function cannot access private member

From Dev

Can we call private methods of abstract class using reflection?

From Dev

Why can't a PRIVATE member function be a friend function of another class?

From Dev

why can't my class friend function access a protected member with namespaces?

From Dev

can't access member even after making a member function a friend

From Dev

Member function of a class as friend to another class

From Dev

Making member function, friend of a class

From Dev

C++ Accessing a private member in a friend class

From Dev

Private base class affects member access via template friend function and template method in child class

From Dev

Templated Class Friend Operator Member Function

From Dev

Can a private static member be used as a default argument to a member function of its class?

From Dev

Friend ostream can't access private member

From Dev

Can we declare a friend function with no argument?

From Dev

Accessing member functions of other classes into member function of `this` class using composition or friend classes

From Dev

Friend function of a private inner class

From Dev

friend function can't access private struct

From Dev

How a friend class can access a private member of a nested class?

From Dev

Why can't I access the private variable of a class after defining a friend function?

From Dev

Why aren't we allowed to access a private static member outside class without public member function?

From Dev

Friend function can't access private members

From Dev

C++ Accessing a private member in a friend class

From Dev

Friend functions and namespaces. Cannot access private member in class

From Dev

Using Member Functions in a Friend Function

From Dev

Friend ostream can't access private member

From Dev

Not able to access private member through friend function

Related Related

  1. 1

    Private function as friend of other class

  2. 2

    Why can't this public member function call decltype on a private struct member declared inside the class?

  3. 3

    Friend function of a private inner class

  4. 4

    friend for private class

  5. 5

    Friend member function cannot access private member

  6. 6

    Can we call private methods of abstract class using reflection?

  7. 7

    Why can't a PRIVATE member function be a friend function of another class?

  8. 8

    why can't my class friend function access a protected member with namespaces?

  9. 9

    can't access member even after making a member function a friend

  10. 10

    Member function of a class as friend to another class

  11. 11

    Making member function, friend of a class

  12. 12

    C++ Accessing a private member in a friend class

  13. 13

    Private base class affects member access via template friend function and template method in child class

  14. 14

    Templated Class Friend Operator Member Function

  15. 15

    Can a private static member be used as a default argument to a member function of its class?

  16. 16

    Friend ostream can't access private member

  17. 17

    Can we declare a friend function with no argument?

  18. 18

    Accessing member functions of other classes into member function of `this` class using composition or friend classes

  19. 19

    Friend function of a private inner class

  20. 20

    friend function can't access private struct

  21. 21

    How a friend class can access a private member of a nested class?

  22. 22

    Why can't I access the private variable of a class after defining a friend function?

  23. 23

    Why aren't we allowed to access a private static member outside class without public member function?

  24. 24

    Friend function can't access private members

  25. 25

    C++ Accessing a private member in a friend class

  26. 26

    Friend functions and namespaces. Cannot access private member in class

  27. 27

    Using Member Functions in a Friend Function

  28. 28

    Friend ostream can't access private member

  29. 29

    Not able to access private member through friend function

HotTag

Archive