Friend class?

This is a discussion on Friend class? within the C++ Programming forums, part of the General Programming Boards category; I have 2 classes: Database and DatabaseMgr . The functions in DatabaseMgr need to access the variables in Database . ...

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Friend class?

    I have 2 classes: Database and DatabaseMgr. The functions in DatabaseMgr need to access the variables in Database. To my understanding, I can declare DatabaseMgr a friend class to Database or inherit DatabaseMgr from Database.

    Code:
    class Database
    {
        friend class DatabaseMgr;
        public:
        /*constructors/destructors*/
      
        protected:
        /*member variables*/
    };
    ....or

    Code:
    class DatabaseMgr:public Database
    {
        public:
        /*functions that access the member variables of Database
    };
    So my question is, whats the difference? Do both methods give me the same outcome?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    When reaching for public inheritance, ask the question: is my intended derived class a subtype of my intended base class? That is, can you say that a DatabaseMgr is-a Database? From the names, I would guess that a DatabaseMgr is associated with a Database, but a DatabaseMgr is not a type of Database. Consequently, you should not use public inheritance.

    In fact, even if you did use inheritance (whether public, protected or private), the DatabaseMgr would still be unable to access the private members of the Database. Oh yes, you can declare your member variables as protected, but consider that anyone can inherit from your Database class, so even though you may be the maintainer of both the Database and DatabaseMgr class, you now have the internals of your Database class fixed in stone unless you want to break API compatibility.

    As such, friendship may be the more appropriate relationship. If the DatabaseMgr class only has say, one particular function that needs to know about the internals of the Database, then consider making that function (instead of the entire DatabaseMgr class) a friend of the Database class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    That makes alot of sense. Thanks alot! I will go with the friendship method instead of using inheritance.

    [edit]
    In fact after reading your post, I now think that there is no need for a DatabaseMgr class as I really don't have to add, remove (or any type of them methods) a database. Therefore a need doesn't exist for a friend class to be created, still thanks alot though [/edit]
    Last edited by legit; 11-25-2008 at 10:03 AM.

  4. #4
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Now I think I'm confused :S

    Instead of having a DatabaseMgr class, which in my last, not so successful database, is where everything was going on. All the menu's and functions were all crammed into that 1 class.

    Now that i've gotten rid of it, i'm using a ResourceMgr class to control all the menu's, and i'm thinking of a way to work a Funding system, to keep track of the profit (if any) of the company and so on. I'm going to create a FundingMgr class to handle all the income and outcome. I will overload some operators(+,-,*) etc. I will also try to work a file system and a login/logout system. But now I have no real need for the Database class at all. I'm stumped as what to do now with the class.

    Should I get rid of it like the DatabaseMgr class? Sorry i've posted in the same thread, I just thought it would save the hassle and the room of creating a new thread.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    If you new design can avoid friends, I would probably say it's worth it. Keep your database class around in commented out form so you don't need to rewrite anything in case it doesn't work out.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Elysia View Post
    If you new design can avoid friends, I would probably say it's worth it.
    Why would you say that? Is having a friend function or class a bad thing?

    Quote Originally Posted by Elysia View Post
    Keep your database class around in commented out form so you don't need to rewrite anything in case it doesn't work out.
    Will do

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I say that because friends can easily break encapsulation, and it creates a dependency, and you want to avoid those.
    Because if class Y depends on class X, when class X changes, then so must class Y. But if they are not dependant on each other, then if you change class X, then class Y will not need to change.

    Friends are a powerful tool, but it can easily be misused and create code that breaks easily.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Elysia
    Keep your database class around in commented out form so you don't need to rewrite anything in case it doesn't work out.
    Heh, but then there should be nothing to worry about with proper version control

    Quote Originally Posted by legit
    Is having a friend function or class a bad thing?
    Not necessarily, but the more functions that have access to the internals of the class (and that includes member functions), the less robust the class is to a change in its internals, since you may have more functions to change should you change the internal implementation of the class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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