472,146 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Inheriting friends

Hello,
I am working in a framework with certain restrictions; in particular I
have a 'data' class with getter and setter methods, and a some 'filling'
classes which know how to insert the data to the data class (they match
various sources such as a text file or a database, and then fill the
data).
The filling methods use the 'setter' methods of the data class, but then
I want to present the data class to the outside world (i.e. other
classes in the program) such that the setter methods cannot be used by
anyone else.

The sledgehammer approach would be to declare the 'setter' methods
private or protected and declare each of the fillers as a friend.
However, I am free to introduce some inheritance structures... if I make
a base class for the fillers and declare *it* to be a friend, will the
derived classes also be friends?

Is this reasonable?
Dec 12 '06 #1
3 1587
* shaun roe:
Hello,
I am working in a framework with certain restrictions; in particular I
have a 'data' class with getter and setter methods, and a some 'filling'
classes which know how to insert the data to the data class (they match
various sources such as a text file or a database, and then fill the
data).
The filling methods use the 'setter' methods of the data class, but then
I want to present the data class to the outside world (i.e. other
classes in the program) such that the setter methods cannot be used by
anyone else.

The sledgehammer approach would be to declare the 'setter' methods
private or protected and declare each of the fillers as a friend.
However, I am free to introduce some inheritance structures... if I make
a base class for the fillers and declare *it* to be a friend, will the
derived classes also be friends?
No.

Is this reasonable?
No.

Data is just data.

The 'const' keyword helps to prevent modification.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 12 '06 #2

shaun roe wrote:
Hello,
I am working in a framework with certain restrictions; in particular I
have a 'data' class with getter and setter methods, and a some 'filling'
classes which know how to insert the data to the data class (they match
various sources such as a text file or a database, and then fill the
data).
The filling methods use the 'setter' methods of the data class, but then
I want to present the data class to the outside world (i.e. other
classes in the program) such that the setter methods cannot be used by
anyone else.

The sledgehammer approach would be to declare the 'setter' methods
private or protected and declare each of the fillers as a friend.
However, I am free to introduce some inheritance structures... if I make
a base class for the fillers and declare *it* to be a friend, will the
derived classes also be friends?

Is this reasonable?
You can't "inherit" friends. Friends are friends of what they were
declared to be friends of, and nothing more.

However, if you declare your "setter" methods to be protected and
virtual, then it doesn't really matter that you can't inherit friends.
The friend "filler" doesn't need to know that it's actually operating
on a descendant of the class it thinks it's working with: it's working
with it's friend, and the fact that it's really a virtual method
defined by a descendant doesn't enter into play.

If you don't plan on overriding the method, you don't even need to
declare it virtual. It will be the base class's method that gets
called, as long is it doesn't get hidden by another member function
with the same name.

A design that involves having to add a new "friends" declaration
somewhere each time you want to extend functionality with a new type of
setter is almost certainly the wrong way to go about it. Better might
be to declare one "filler" class to be a friend, make the appropriate
methods protected, and derive from that class. Again, it doesn't matter
that the derivatives aren't friends, so long as you're only ever using
the base class's member function anyway.

As to whether this is reasonable, I couldn't begin to comment on that
without knowing what your particular problem or situation is. What I
will tell you is, code for the future. Leave as much flexibility for
change open to yourself, while at the same time guarding as much as
possible/practical against poor usage.

Dec 12 '06 #3

Micah Cowan wrote:
shaun roe wrote:
Hello,
I am working in a framework with certain restrictions; in particular I
have a 'data' class with getter and setter methods, and a some 'filling'
classes which know how to insert the data to the data class (they match
various sources such as a text file or a database, and then fill the
data).
The filling methods use the 'setter' methods of the data class, but then
I want to present the data class to the outside world (i.e. other
classes in the program) such that the setter methods cannot be used by
anyone else.

The sledgehammer approach would be to declare the 'setter' methods
private or protected and declare each of the fillers as a friend.
However, I am free to introduce some inheritance structures... if I make
a base class for the fillers and declare *it* to be a friend, will the
derived classes also be friends?

Is this reasonable?

You can't "inherit" friends. Friends are friends of what they were
declared to be friends of, and nothing more.

However, if you declare your "setter" methods to be protected and
virtual, then it doesn't really matter that you can't inherit friends.
The friend "filler" doesn't need to know that it's actually operating
on a descendant of the class it thinks it's working with: it's working
with it's friend, and the fact that it's really a virtual method
defined by a descendant doesn't enter into play.

If you don't plan on overriding the method, you don't even need to
declare it virtual. It will be the base class's method that gets
called, as long is it doesn't get hidden by another member function
with the same name.

A design that involves having to add a new "friends" declaration
somewhere each time you want to extend functionality with a new type of
setter is almost certainly the wrong way to go about it. Better might
be to declare one "filler" class to be a friend, make the appropriate
methods protected, and derive from that class. Again, it doesn't matter
that the derivatives aren't friends, so long as you're only ever using
the base class's member function anyway.

As to whether this is reasonable, I couldn't begin to comment on that
without knowing what your particular problem or situation is. What I
will tell you is, code for the future. Leave as much flexibility for
change open to yourself, while at the same time guarding as much as
possible/practical against poor usage.
Dec 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by richardlane | last post: by
15 posts views Thread by JustSomeGuy | last post: by
29 posts views Thread by shaun roe | last post: by
11 posts views Thread by Noah Coad [MVP .NET/C#] | last post: by
2 posts views Thread by Charles Law | last post: by
3 posts views Thread by Alex Satrapa | last post: by
3 posts views Thread by srinivasan srinivas | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.