Hello people,
I have two classes everyone with private data.
I want only one function from each class to access private data from another class. I have checked "friend" function but It needs the class object as an argument. That is making things complicated on my side and I have no idea on how to do this.
Is this possible? If yes, how can I archieve that?
Thanks alot

If the private data is static, i.e. all instances of the class share the static private data, then there is no need for the class object as an argument.

If the private data is non-static, then you must have the class object as an argument because the object instance actually contains the data.

So can you explain a little bit on Static thing?

Essentially a static member variable (be it of any type) is a variable that is common to all non-static instances of the class.
Maybe the following explains it a bit ..

#include<iostream>
using namespace std;

// forward declaration
class B;

class A
{
  // 'shared_value' is common to all instances
  // of class A because it is 'static'
  static int shared_value;

public:
  A(){}

  void print()
  {
    // Display the current value
    cout << "A::shared_value = " << shared_value << endl;
  }

  // class B is granted access
  friend B;
};

class B
{
public:
  B(){}

  void set(int ii)
  {
    // Modify the A class' private value
    A::shared_value = ii;
  }
};

// Upon program start-up, initialize ..
int A::shared_value = 0;

int main()
{
  A a1;
  B b;

  // The initial value should be 0 ..
  a1.print();

  // Change the A's data through B
  b.set(42);

  // The new value is .. 
  a1.print();

  A a2;

  // a2 shares the value with a1 ..
  a2.print();

  cin.get();
}

can I use combination of static variable and friend function to expose that variable to only one method of another class?

Thanks sir

can I use combination of static variable and friend function to expose that variable to only one method of another class?

Yes you can, like so ..

#include<iostream>
using namespace std;

// forward declaration
class A;
class B
{
public:
  B(){}
  void set(int ii);
};

class A
{
  // 'shared_value' is common to all instances
  // of class A because it is 'static'
  static int shared_value;

public:
  A(){}

  void print()
  {
    // Display the current value
    cout << "A::shared_value = " << shared_value << endl;
  }

  // only B::set(int ii) is granted access
  friend void B::set(int ii);
};

// Upon program start-up, initialize ..
int A::shared_value = 0;

void B::set(int ii)
{
  // Modify the A class' private value
  A::shared_value = ii;
}

int main()
{
  A a1;
  B b;

  // The initial value should be 0 ..
  a1.print();

  // Change the A's data through B
  b.set(42);

  // The new value is .. 
  a1.print();

  A a2;

  // a2 shares the value with a1 ..
  a2.print();

  cin.get();
}

Thanks alot.
My head is now very tired and is as if it want to stop thinking. I will rest and apply your ideas and see where I get. One last Question. Can this Static variable be a pointer? (I saw that description on your top post but I want to be sure of that). And also small good tutorial on static variables/methods is appreciated

Thanks alot

Can this Static variable be a pointer?

Yes it can. About small tutorials, I'd bet that you'll find reasonable ones by searching the web for example "static member variable in c++".

Essentially a static member variable (be it of any type) is a variable that is common to all non-static instances of the class.
Maybe the following explains it a bit ..

#include<iostream>
using namespace std;

// forward declaration
class B;

class A
{
  // 'shared_value' is common to all instances
  // of class A because it is 'static'
  static int shared_value;

public:
  A(){}

  void print()
  {
    // Display the current value
    cout << "A::shared_value = " << shared_value << endl;
  }

  // class B is granted access
  friend B;
};

class B
{
public:
  B(){}

  void set(int ii)
  {
    // Modify the A class' private value
    A::shared_value = ii;
  }
};

// Upon program start-up, initialize ..
int A::shared_value = 0;

int main()
{
  A a1;
  B b;

  // The initial value should be 0 ..
  a1.print();

  // Change the A's data through B
  b.set(42);

  // The new value is .. 
  a1.print();

  A a2;

  // a2 shares the value with a1 ..
  a2.print();

  cin.get();
}

In the main function,you've gotten the object of class A,why still choose the static variable.If you want operate the object of A,the object must have been created,so why you can't get the object?

#include<iostream>
unsing namespace std;
class A{
    int i;
    public:A():i(0){}
              friend void set(A& a,int i){
                                a.i = i;
               }
             void print(){
                      cout<<this->i<<endl;
             }
}

int main(){
    A a;      /// you get the object
    int input =0;
    cin>>input;
    set(a,input);
    
    system("pause");

    return 0;
}

And also,you can put the function set() in a class named B,take the B as a friend class of A

In the main function,you've gotten the object of class A,why still choose the static variable.

If you go through estevemd's posts, maybe you get the main idea of what's been shown above. I.e. that it is possible to have a private static (<- because estevemd rather has it that way) member variable, which can be accessed by another class' member method.

Then, sorry but I don't actually quite understand are you asking a question or demonstrating something (or both)?

If you go through estevemd's posts, maybe you get the main idea of what's been shown above. I.e. that it is possible to have a private static (<- because estevemd rather has it that way) member variable, which can be accessed by another class' member method.

Then, sorry but I don't actually quite understand are you asking a question or demonstrating something (or both)?

Wow...
I just gave some codes to show what i thought

Wow...
I just gave some codes to show what i thought

OK, but you see, I really did not understand whether you were asking a question or what, but since you weren't, then it's all OK by me :)

OK, but you see, I really did not understand whether you were asking a question or what, but since you weren't, then it's all OK by me :)

aha.
you know,I preferred the answers above, in contrast,i gave another way to operate the private variable .
Thanks your advice;)

This article has been dead for over six months. Start a new discussion instead.