I am learning about friends in classes and my problem is: I want funcC3() to set and permanently change C1::a value, how to do it? I want global function gfunc() to be able to do the same, how? Could you please provide me the way i do it, because in the book they don't specify?
#include <iostream>
using namespace std;
class C2;
class C3
{
public:
void funcC3(int const& x)
{
cout << "in third class...: " << x << endl;
};
};
class C1
{
private:
int a;
public:
void funcC1()
{
cout << "inside C1 private thingie: " << a << endl;
};
int C1::getcha();
friend class C2;
friend int gfunc(int chair);
friend void C3::funcC3(int const& a);
};
int C1::getcha()
{
return a;
};
class C2
{
public:
int a;
};
**int gfunc(int ch)**
{
int chair = ch;
return chair;
};
**int main()**
{
C1 obj1;
C3 obj3;
obj3.funcC3(10);
obj1.funcC1();
gfunc(12);
cout << C1.geta() << endl;
system("pause");
}
I want funcC3() to set and permanently change C1::a value, how to do it?
Declare it as friend function in C1:
class C1; //forward declaration
class C3
{
//...
void funcC3( C1& c);
};
class C1
{
//...
private:
int a;
friend void C3::funcC3( C1&);
};
void C3::funcC3( C1& c) { c.a = 100;}
I want global function gfunc() to be able to do the same, how?
Same as above:
class C1;
void gfunc( C1& c);
class C1 {
//...
private:
int a;
friend void gfunc( C1& c);
};
void gfunc( C1& c) { c.a = 100;}