We cannot have static friend function............ because

1.since static member functions can access only static data members...where as friend function can access all the members.

2.Friend function also require an object to access the members...where as static member functions can not.

3. Another thing ....since friend functions are non-member functions....and static functions are member functions

if any other answers please let me know

Recommended Answers

All 6 Replies

>>We cannot have static friend function............

who told you that mis-information????

Is below the sort of thing you are talking about? My compilers do not have a problem with static friend. And if you google for "static friend functions" you will find more info about it, such as this thread.

#include <iostream>
using namespace std;

class foo;

class foo1
{
public:
	static friend foo;
	foo1() {x = 1;}
	int getx() {return x;}
protected:
	int x;
};


class foo
{
public:
	foo() {k.x = 2;}
	foo1 k;
};


int main(int argc, char* argv[])
{
	foo f;
	cout << f.k.getx() << endl;
	return 0;
}

Hm.. Mr. Dragon, I think there is a typo mistake coz the code will not compile. And what the OP wants is "static friend function".

@Madankumar:

By static do you mean "static storage class" or "static linkage" ?
If you mean "static linkage" then friend function can be static.

As far as static storage class friend functions are concerned, you can have a class with static functions which is a friend of another class. But there is no need since friend functions can access all the variables of the given class whether static or non static

#include <iostream>
using namespace std;

class Mine
{
    public:
        string mystring ;

        Mine()
        {
            mystring = "hello" ;
            i++ ;
        }
    friend void display( ) ;

    private:
            static int i ;
};

int Mine::i = 0 ;

void display( )
{
    Mine var ;
    cout << Mine::i  << endl;
    cout << var.mystring ;
}

int main( )
{
    Mine i , j ;
    display(  ) ; // will output 3
    return 0;
}

MyOutput:

3
hello

Hm.. Mr. Dragon, I think there is a typo mistake coz the code will not compile. And what the OP wants is "static friend function".

I compiled it with both both VC++ 6.0 and VC++ 2005 Pro, neither complained. But I just now tried it with Dev-C++ and got an error on that line I posted in red. Maybe another famous M$ bug!

Below is a reprint of the example taken from the link to the thread I posted earlier. OP sould read the full text of that link for explaination.

#include "stdio.h"
#include "stdlib.h"

class A;

class B
{
public:
    B();
    ~B();

    static void SetAiPrivate(int value);
    static A *pa;
};

class A
{
friend void B::SetAiPrivate(int);

public:
    A(){iPrivate = 0;}
    ~A(){}
    void PrintData(){printf("iPrivate = %d\n", iPrivate);}

private:
    int iPrivate;
};


A *B::pa;

B::B()
{
    pa = new A;
}

B::~B()
{
    delete pa;
}

void B::SetAiPrivate(int value)
{
    pa->iPrivate = value;
}

int main()
{
    B b;

    B::SetAiPrivate(7);
    B::pa->PrintData();
}

Hmm the line in red says: static friend foo; But as far as i know, to declare a class a friend of another, we just need friend foo; coz the qualifier static doesnt make sense in that context ( for explanation please read my prev post ).

And compiling the same eg. with the recent version of GCC MingW compiler flags the same error. Maybe static qualifier before the friend keyword are not allowed in C++ standards. :confused:

I got the same error with Dev-C++ (uses the same compiler). The Microsoft compilers did not complain, but seems to just silently ignore that static keyword.

Heh, maybe a feature of their "smart" and "optimizing" compiler :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.