Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can static member funcs be friends?

Reply
Thread Tools

Can static member funcs be friends?

 
 
Improving
Guest
Posts: n/a
 
      10-06-2005
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;
}

 
Reply With Quote
 
 
 
 
sat
Guest
Posts: n/a
 
      10-06-2005
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" <> wrote in message
news: 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;
> }
>



 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      10-06-2005
On 6 Oct 2005 03:19:14 -0700, "Improving" <>
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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-06-2005
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;
> }


 
Reply With Quote
 
Improving
Guest
Posts: n/a
 
      10-06-2005
OMFG what a plonker I am!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can pydoc display doc for local funcs? or other doc tools for owncode News123 Python 0 02-06-2010 12:31 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Ptrs to member funcs & inheritance Thomas Matthews C++ 2 12-20-2003 09:04 PM
Creating table of const ptr to member funcs Thomas Matthews C++ 6 11-03-2003 07:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57