Is this a good use of friend?

  • Thread starter anongroupaccount
  • Start date
A

anongroupaccount

I have a smaller class that essentially acts as a record for a larger
class (stored in a vector inside of the larger class). In a sense, it's
splitting functionality over two classes.

I don't want any other classes to be able to use this smaller class, as
it just wouldn't make sense. I figured that I could either declare the
smaller class inline in my larger class (not keen on doing that as it
makes a mess!) or give the smaller class private members and make the
larger class a friend so only it can use it.

Is this a good use of friend?
 
B

Bob Hairgrove

I have a smaller class that essentially acts as a record for a larger
class (stored in a vector inside of the larger class). In a sense, it's
splitting functionality over two classes.

I don't want any other classes to be able to use this smaller class, as
it just wouldn't make sense. I figured that I could either declare the
smaller class inline in my larger class (not keen on doing that as it
makes a mess!) or give the smaller class private members and make the
larger class a friend so only it can use it.

Is this a good use of friend?

It sounds like it might be a good use -- judicious use of friend
classes can improve encapsulaton and OOP. Subsystems, for example, can
be aggregates of classes which together fulfill a common
responsibility. There might be no need for classes outside the
subsystem to have access to things that the other classes within the
subsystem would need to access, so there you would probably need some
friends.

Even if you declare the record class as a nested class, you would need
to make the outer class a friend of the inner class if it has private
members of its own.
 
R

roberts.noah

I figured that I could either declare the
smaller class inline in my larger class (not keen on doing that as it
makes a mess!)

I actually prefer that method as it is closer to how it is being used.
Unless of course I am using the internal class in other classes as
well...but then inner class isn't what I mean anymore.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top