Static Friend Function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mini Singh
    New Member
    • Sep 2006
    • 10

    #1

    Static Friend Function

    Is it possible to make Friend function Static???
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Mini Singh
    Is it possible to make Friend function Static???
    Yes, I think without the befinit of context

    Comment

    • deepakpb
      New Member
      • Sep 2006
      • 5

      #3
      No we cant make the friend function as static, bcoz Static member function can used only the static data member and friend function can use the private data member not the static data members. The static data member is having global scope but visible only within the class.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Nobody said it was a member function, only that it was static.

        A static member function can only access static data members of it's own class, this does not preclude it from accessing any data members of another class and would not stop it accessing private members where it declared as a firend.

        Code:
        #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();
        }

        Comment

        • vermarajeev
          New Member
          • Aug 2006
          • 180

          #5
          Hi Banfa,
          I'm glad by your solution..Can you please explain me your program above.
          Also I have made some modification here. Please explain me the difference. I'm still confused about friend functions.....

          I need your help to understand the concept....

          Code:
          class A;
          
          class B
          {
          public:
          	B();	
              static friend void setValue(int val);
          	static A* pa;
          };
          
          class A
          {
          	static friend void setValue(int val);
          	int ia;
          public:
          	A():ia(0){}  
          	void display()
          	{
          		cout<<"Data:"<<ia<<endl;
          	}
          };
          
          B::B()
          {
          	pa = new A;
          }
          
          A* B::pa = 0;
          
          void setValue(int val)
          {
          	B b;
          	b.pa->ia = val;
          }
          
          int main(int argc, char** argv[])
          {
          	B b;	
          	setValue(5);
          	B::pa->display();
          	return 0;
          }

          Comment

          • vermarajeev
            New Member
            • Aug 2006
            • 180

            #6
            Hi banfa,
            I'm glad by your solution....

            Can you elaborate about what you have written the code above...

            Also I have done some modifications in your program....
            Can you explain me the difference...
            I'm confused by the term friend and needs to clear about that

            Code:
            class A;
            
            class B
            {
            public:
            	B();	
                static friend void setValue(int val);
            	static A* pa;
            };
            
            class A
            {
            	static friend void setValue(int val);
            	int ia;
            public:
            	A():ia(0){}  
            	void display()
            	{
            		cout<<"Data:"<<ia<<endl;
            	}
            };
            
            B::B()
            {
            	pa = new A;
            }
            
            A* B::pa = 0;
            
            void setValue(int val)
            {
            	B b;
            	b.pa->ia = val;
            }
            
            int main(int argc, char** argv[])
            {
            	B b;	
            	setValue(5);
            	B::pa->display();
            	return 0;
            }
            Eagerly Waiting for your reply

            Comment

            Working...