Thread: hi, is it possible to access the prviate members of a class using a friend function?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    hi, is it possible to access the prviate members of a class using a friend function?

    of that class too?

    ie,

    class afriend
    {
    friend void alterdata();
    private:
    int data;
    };

    so how do i fix this?

    void alterdata()
    {
    data = 5; // error
    }


    thanks!

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Straight from Sams in 21 days.....

    "If you want to expose your private member data or functions to another class, you must declare that class to a be a friend. This extends the interface of your class to include the friend class."

    Remember just because you declare a class to be a member of another doesn't mean that class can access the other classe's method.

    For example, I declare you to be my friend, I decide to tell you might secret, that's my business I don't have to have access to your secrets.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    um, er., i think that's not the answer to my question?

    my example, alterdata() which is a friend function, and upon the implementation of it, the compiler will output an error, so how do i fix this?

    there's only one class here...,

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Code:
    class afriend { 
            friend void alterdata(); 
        private: 
            int data; 
    }; 
    
    void alterdata() 
    { 
    afriend::data = 5; 
    }
    Remember that friend functions are NOT members of the class. That's why the alterdata function definition is not written:

    afriend::void alterdata()
    {
    data = 5;
    }

    But they DO have access to all members of the class. You just have to make sure you qualify those members with the class name.

    [EDIT]
    Just corrected a small mistake. It's ok now.
    [/EDIT]
    Last edited by Mario; 05-28-2002 at 10:49 AM.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    so a scope resolution does the job, thanks!!! no wait..., i've suddenly thought about this, that doesn't make any sense at all, coz, i mean, it doesn't belong to any object right? so, i'm assigning a value to a data (which is 5) but in what object does data belong??

  6. #6
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by mickey
    no wait..., i've suddenly thought about this
    Considering I wrote that about a week ago, I would say your "suddenly" is way slow

    But nonetheless you are right, of course. It just happens I only had your example to go through. Still, I went on the err side because i didn't notice the following:

    What I wrote can be done when the data member (private or not) was declared as static. These kind of data members exist even before any object gets created.

    Otherwise, you should pass the object as an argument of the function.

    void alterdata(afriend myfriend)
    {
    myfriend.data = 5;
    }

    For this minimalist example you give there is in fact no need to declare the function as friend. You should put it on the public section of your class and it becomes a member function of the class.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-17-2005, 12:55 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM