473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can static member funcs be friends?

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;
}

Oct 6 '05 #1
4 1161
Improving wrote:
#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;
}


Oct 6 '05 #2
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.

"Improving" <ru**@haydon449.plus.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... 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;
}

Oct 6 '05 #3
On 6 Oct 2005 03:19:14 -0700, "Improving" <ru**@haydon449.plus.com>
wrote:
why doesnt A::func have access to a B objects internals?
It does ... but you need to make A::func() public in order to call it
from main().
#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;
}


--
Bob Hairgrove
No**********@Home.com
Oct 6 '05 #4
OMFG what a plonker I am!

Oct 6 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
1
by: Alex Vinokur | last post by:
I want static functions of classes Uam and Bar to be friends of class Foo. There is no problem with Uam::uam(). But class Bar has a member of Foo type: member Bar::m_foo, so one can't put Bar...
4
by: sanketbarot | last post by:
Hello Friends, What is the advantage of defining a function as Static as a class member. Like Class Name {
6
by: silverburgh.meryl | last post by:
Hi, In one A.cpp file, I have defined a static array of JSFunctionSpec, like this: static JSFunctionSpec JProfFunctions = { {"JProfStartProfiling", JProfStartProfiling, 0, 0, 0 },...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.