Friend Class

This is a discussion on Friend Class within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I've never used friend classes before, but just today I found (a very large) use for them in ...

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    Friend Class

    Hello everyone. I've never used friend classes before, but just today I found (a very large) use for them in a project of mine. Anyway, what im doing is this:

    Code:
    class test
    {
    public:
    	int tester;
    };
    	
    
    
    class Cblah
    {
    public:
    	int type;
    	friend test;
    };
    
    ...(later on)
    Cblah bob;
    
    ...(later)
    if(bob.tester==1)
    {
        ...
    }
    But when I try to compile it, it tells me that 'tester' is not a member or Cblah. Now, it isn't, but shouldn't the friend to test allow it to access 'tester'? I may be totally wrong about how friends work, so could someone tell me either what im doing wrong or set friends straight? Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Yup, I think your idea of friend classes is totally wrong.

    Friend classes/functions can access the private members of a class. That's basically it. If you specify "friend class Test" in class Blah, then it means that Test can access and manipulate private, protected and public members/functions of class Blah.

    Code:
    class Test
    {
       public:
        void CallBlahFunc(Blah& b)
        {
            b.BlahFunc(); // valid, coz it's a friend of blah
        {
    }
    
    class Blah
    {
       friend class Test;
       private:
        void BlahFunc(void);
    }
    
    Test t;
    Blah b;
    
    b.BlahFunc(); // wont work, private function
    t.CallBlahFunc(); // works no problem
    I hope this clears it up a bit.

    cheers
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Thanks, understand them now!

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163
    I think what you were thinking is inheritance
    +++
    ++
    + Sekti
    ++
    +++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 01-18-2009, 10:53 PM
  2. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 12:29 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21