Can static member funcs be friends?

I

Improving

why doesnt A::func have access to a B objects internals?

#include<iostream>
using namespace std;
class A;
class B
{
friend class A;
int a;

public:
B(int b) : a(b)
{;}
};

class A
{
static void func(B* b)
{
cout<< b->a << endl;
}
};

int main()
{
B b(10);
A::func(&b);
cin.get();
return 0;
}
 
S

sat

Yes , there is nothing wrong with your code. It should run fine, but for one
hiccup.

int main()
{
B b(10);
A::func(&b); // this is an error

func in class A is by default a private member.. (though it is static),
hence your program might have not compiled.
try putting it in public scope. I thnk the program should go thro' fine.
 
R

Rolf Magnus

Improving said:
#include<iostream>
using namespace std;
class A;
class B
{
friend class A;
int a;

public:
B(int b) : a(b)
{;}
};

class A
{
public:

static void func(B* b)
{
cout<< b->a << endl;
}
};

int main()
{
B b(10);
A::func(&b);
cin.get();
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top