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 . ...

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.

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.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

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]

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.

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.

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

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.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.