Thread: Freind function

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Freind function

    Code:
    class A
    {
       private:
          int a;
       punlic:
          friend ostream & operator<<(ostream &os, const A &object);
         
    };
    ostream & operator<<(ostream &os, const A &object)
    {
               os <<  object.a;
               return osl;
    }
    
    
    class B: public class A
    {
         public:
             freind ostream & operator<<(ostream &os, const B &object)
    {
         os <<  object.a ;
    }
    };
    In class A, I have the friend function, and use it to print out the private instance variable a.
    Now, in class B, I would like to print out the variable a, which is inherited from class A, but I get an error when I try objcet.a, in class B, How can I access the variable a, using the friend function in class B?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You have lot of spelling errors but I'm sure you are fully aware of them. You need to make the variable in class A "protected" not private. Only protected and public members and methods get inherited. Try that and it should work.

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I didnt want to throw all my code up there, so I just showed what I needed.

    One more question. If I make them protected they will be accessible to derived classes but not in main?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Correct, main will still NOT be able to access them as if they were private, but your derived classes will be able to access them freely.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    There's no need as I see it to allow the derived class direct access to the base member variables....the only action is to print value (not manipulate it directly), and the functionality is presented fine in the base class...therefore I would leave it as is...

    Also note that B is derived from A...therefore references to B can be passed as a param to operator<<()

    Code:
    #include <iostream>
    using std::ostream;
    using std::cout;
    using std::endl;
    
    
    class A{
    	int a;
        friend ostream& operator<<(ostream &os, A &object);
    public:
    	A(int _a):a(_a){}   
    };
    
    class B: public A{
    public:
    	B(int _b):A(_b){}
    };
    
    ostream& operator<<(ostream &os, A &object){
    	os <<  object.a;
        return os;
    }
    
    int main() {
    	A a(10);
    	B b(15);
    	
    	cout << a << " " << b << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM