C++
  Home arrow C++ arrow Page 4 - C++ Tricks of the Trade: Friend Functions
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

C++ Tricks of the Trade: Friend Functions
By: Kais Dukes
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 66
    2002-06-21

    Table of Contents:
  • C++ Tricks of the Trade: Friend Functions
  • Friendly Friends
  • Commutative Operators
  • Friend Classes
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C++ Tricks of the Trade: Friend Functions - Friend Classes


    (Page 4 of 5 )

    As well as choosing a non-member friend function, a class has two other possibilities for its friends. A class can declare a member function of another class as a friend, or declare another class as a friend class.

    Friend classes are used in cases where one class is tightly coupled to another class. For example, suppose we have a class CPoint that represents a coordinate, and a class CPointCollection that holds a list of points. Since the collection class may need to manipulate point objects, we could declare CPointCollection as a friend of the CPoint class:

    // Forward declaration of friend class.
    class CPointCollection;

    // Point class.
    class CPoint
    {
    friend CPointCollection;

    public:
    CPoint(const double x, const double y) :
    m_x(x),
    m_y(y)
    {
    }

    ~CPoint(void)
    {
    }

    // ...

    private:
    double m_x;
    double m_y;
    };


    Since the collection class is a friend of CPoint, it has access to the internal data of any point object. This is useful when individual elements of the collection need to be manipulated. For example, a set method of the CPointCollection class could set all CPoint elements to a particular value:

    class CPointCollection
    {
    public:
    CPointCollection(const int nSize) :
    m_vecPoints(nSize)
    {

    }

    ~CPointCollection(void);
    void set(const double x, const double y);

    // ...

    private:
    vector<CPoint> m_vecPoints;
    };


    The set member can iterate over the collection and reset each point:

    void CPointCollection::set(const double x, const double y)
    {
    // Get the number of elements in the collection.
    const int nElements = m_vecPoints.size();

    // Set each element.
    for(int i=0; i<nElements; i++)
    {
    m_vecPoints[i].m_x = x;
    m_vecPoints[i].m_y = y;
    }
    }


    One thing worth noting about friend classes is that friendship is not mutual: although CPointCollection can access CPoint, the converse is not true. Friendship is also not something that is passed down a class hierarchy. Derived classes of CPointCollection will not be able to access CPoint. The principle behind this is that friendship is not implicitly granted; each class must explicitly choose its friends.

    More C++ Articles
    More By Kais Dukes


       · A better approach imho is:classB;class A{ A(const & A); A &...
     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT