Using Friends keyword... appropriate?

This is a discussion on Using Friends keyword... appropriate? within the C++ Programming forums, part of the General Programming Boards category; For a class i am currently designing, called Pane, during a method i am using another pane to calculate my ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Using Friends keyword... appropriate?

    For a class i am currently designing, called Pane, during a method i am using another pane to calculate my abs left and top coordinates.

    my absLeft = parent.absLeft;

    Would it be appropriate to make Pane a friend to Pane, so i could just access this directly, or would it be more appropriate to use a method to return the value.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Post the class interface and the code for the function that is performing this calculation please.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    if your class is something similar to this:
    Code:
    class A
    {
    private:
        int someNum;
    public:
        void DoSomething(A foo)
        {
            // Do some stuff
        }
    };
    and you want A::DoSomething(A foo) to have access to someNum in foo it already has that. Every instance of a class can access all memberdata in an instance if they are of the same class. This means you can safely do this

    Code:
    class A
    {
    private:
        int someNum;
    public:
        void DoSomething(A foo)
        {
            someNum = foo.someNum
        }
    };
    Last edited by Shakti; 06-04-2005 at 04:59 PM.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    I ended up just making functions because i wasnt sure.
    Code:
    Interface:
    #include "SDL.h"
    #include <vector>
    using std::vector;
    
    
    
    class Pane
    {
    private:
    	int top,left,level;
    	vector<Pane> * children; // Pointers to Children
    	Uint32 color;
    	Pane * parent;
    	int absLeft, absTop;
    	int drawTestLeft, drawTestTop;
    public:
    	void setTop( int );
    	void SetLeft( int );
    	void setLevel( int );
    	void setColor( Uint32 );
    	void setParent( Pane & );
    	void getParent( Pane * );
    
    	int getAbsLeft();
    	int getAbsTop();
    	void draw();
    	Pane();
    
    
    };
    
    Function
    
    void Pane::setParent(Pane & p)
    {
    	if ( &p != NULL )
    	{
    		this->parent = &p;
    		this->absLeft = p.getAbsLeft() + this->left;
    		this->absTop = p.getAbsTop() + this->top;
    		
    	}
    	
    }

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    just saw your post Shakti. i never knew, good to know, thanks

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    btw:
    this->
    is unneeded as it's implied. So you could have:
    Code:
    parent = &p;
    absLeft = p.getAbsLeft() + left;
    absTop = p.getAbsTop() + top;

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Would it be appropriate to make Pane a friend to Pane, so i could just access this directly, or would it be more appropriate to use a method to return the value.
    Iamian, you don't need to use friends in this situation. In fact, you don't have to use anything but direct access. Within Pane::setParent, p and "this" are of the same class. What you want for your clases is to use only public methods for the behavior that other classes use. Sometimes you need to use friends but the cases are rare. Two common cases are operator overloading and implicit types(ie., you're writing a Complex number class, overloading operator+, and want to be able to write 5 + Complex(3, 4) and have the 5 implicitly convert to a complex numbe) and iterators.

  8. #8
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Thanks for all your responces..
    Educational
    Last edited by Iamien; 06-04-2005 at 05:32 PM.

  9. #9
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    I come back with another question
    I want to make a class in my project, to function as an "Artist" i plan to pass this class around to difference objects to have it draw them. Should i make him Friends with the classes? so that i can avoid the overhead in calling functions to get the variables <or should i inline the functions>
    there would be about 4 function calls per object, each time it was drawn. everything that has to be drawn will be drawn so i believe it's an O(n)
    Any advice or suggestions would be great

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    want to make a class in my project, to function as an "Artist" i plan to pass this class around to difference objects to have it draw them. Should i make him Friends with the classes? so that i can avoid the overhead in calling functions to get the variables <or should i inline the functions>
    The function overhead can be mitigated by using inline functions. But for using friends the way you've outlined, I can't think of a situation. Probably better off using a "struct" and having all the data public than using a lot of friends.

    there would be about 4 function calls per object, each time it was drawn. everything that has to be drawn will be drawn so i believe it's an O(n)
    Retrieving the data from the classes is usually so fast compared to the io drawing functions that it's usually a non-issue.

  11. #11
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Alright thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template keyword needed
    By George2 in forum C++ Programming
    Replies: 15
    Last Post: 03-05-2008, 03:07 AM
  2. keyword replacement
    By JackBear in forum C Programming
    Replies: 11
    Last Post: 05-18-2007, 10:54 PM
  3. Virtual Keyword
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2006, 11:37 AM
  4. 'new' keyword
    By pktcperlc++java in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 08:31 PM
  5. how to search a keyword in C?
    By kreyes in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 07:22 PM

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