Thread: friend

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    friend

    I saw this header fil in some code I downloaded :
    Code:
    class car
    {
      friend void start();
      friend void resume();
    }
    void start();
    void resume();
    I dont understand the use of the friend, whats the difference in functionality between the above and the below code ?

    Code:
    class car
    {
      void start();
      void resume();
    }
    Appreciate any input.

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    The friend function is non-menber function that has the right to access provate data members of the class it has been declared to be a friend.

    in the second code the functions you wrote are normal member functions.

    What exactly is it that you are not understanding?
    none...

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    //header file
    
    class Ccar
    {
       friend void Run(Ccar *);
       protected:
       Ccar(size_t Dummy = 0);
       ~Ccar();
       virtual void Routine() = 0;
       private:
       queue<Ccar *>rqueue;
    };
    void Run(Ccar *);
    
    
    //source file
    void Run(Ccar *obj)
    {   
        cout << "*Run*" << endl;
        rqueue.push(obj); <--- rqueue undeclared !!!!!!
    }
    Whats the problem here ?

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    // header fil
    class Ccar
    {
        queue<Ccar*>rqueue;
        void Run(Ccar*);
        friend void Run1(Ccar*);
        protected:
        Ccar(size_t Dummy = 0);
        ~Ccar();
        virtual void Routine() = 0;
    };
    void Run1(Ccar*);
    
    //source file
    void Ccar::Run(Ccar*obj)
    {   
        cout << "*Run*" << endl;
        cout << rqueue.empty() << endl;
    }
    
    void Run1(Ccar*obj)
    {   
        cout << "*Run1*" << endl;
        cout << rqueue.empty() << endl;
    }
    If I use Run then I get 'implicit declaration of fuction Run(...)'
    If I use Run1 then I get 'rqueue undeclared'

    What am I doing wrong here ?

    Thanks for any input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 04:44 PM
  2. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  3. Problem with friend functions in templated linked list
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 04:24 PM
  4. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 10:53 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM