471,071 Members | 9,562 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Nested Class vs Inheritance


Hello All,
When is nested class more preferable that Inheritance ? I think with proper
inheritance and friend class concept we can get the same flexibility as nested
classes
Any comments ..
Best Regards
KInd
--
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/
Jul 22 '05 #1
4 3632

"KInd" <no************@f-m.fm> wrote in message
news:20*******************@foorum.com...

Hello All,
When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes
Any comments ..
Best Regards
KInd


Using inheritance or nested class depends very much on what you want to
achieve and how your logical structure (design) looks like.
One example that comes to my mind where nested classes are the method of
choice (IMHO) is an implementation of a generic class which can be used to
obtain heterogeneous containers with the standard library containers.

It would look something like this:

class CGenericObj {

public:
template<typename T>
CGenericObj( const T& WrappedObj) : m_pWrapper( new CWrapper<T>(
WrappedObj ) ) {};
...
class CBaseWrapper {
....
};

template<typename T>
class CWrapper : public CBaseWrapper {
...
}

};

In this case for example inhertiance simply doesnt make sense logically,
because CWrapper is not a CGenericObj but simply a helper. However, it is a
CBaseWrapper and hence inheritance is perfectly okay.

Regards
Chris
Jul 22 '05 #2
KInd wrote:
When is nested class more preferable that Inheritance ? I think with proper
inheritance and friend class concept we can get the same flexibility as nested
classes
Any comments ..


Nesting of types has a very particular meaning. The nested type
is subordinate to the enclosing one. Its application is essentially
_only_ within the scope of its "parent". Can you misuse that? Yes,
just like anything else. However, properly used it's not in any way
a substitution or even comparable to inheritance.

The connection that exists between the base class and the derived
class and also the connection between them and the rest of the model
have nothing to do with scope, really. Inheriting publicly implies
"is-a" relationship. There are implicit conversions based on the
fact of inheriting the base class. Nothing of that exists between
"enclosing/nested" classes.

So, you cannot really prefer nesting over inheritance since they
are IMO orthogonal (although you cannot really apply both at the same
time).

Victor
Jul 22 '05 #3

"KInd" <no************@f-m.fm> wrote in message
news:20*******************@foorum.com...

Hello All,
When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes
Any comments ..
Best Regards
KInd


Well, there are a few types of inheritance. And the subject matter is large
and difficult to grasp, especially for beginners. I'd recommend reading
Scott Meyers' book "Effective C++", which does a good job describing the
different inheritance strategies.

(I'm not sure where friend-ship comes into play in regards to inheritance,
though...?)

But for a short (attempt at an) answer:

Any time you use public inheritance, you should make sure that any function
or object that expects a base class object will also be satisfied by a
derived class obejct as well, because the derived class object "is a" base
class object. If you're creating a class that "is a" base class (such as a
math student "is a" student), then inheritance is fine.

If your new class needs to utilize the behavior of the base class, but can't
actually be substituted for a base class object (even if just in one special
case), then layering (using a nested class) is a better approach. That's
referred to as the "is implemented in terms of" relationship.

And of course, the obvious use of layering is for something simple like "a
computer has a cpu", so your computer class would contain a cpu object (or
perhaps a pointer to one, asuming the cpu is replaceable :-)). That's call
the "has a" relationship.

(I won't even go nto private or protected inheritance.)

Again though, I'd recommend a good read on the subject.

Hope this helps,
-Howard

Jul 22 '05 #4
KInd <no************@f-m.fm> wrote in message news:<20*******************@foorum.com>...
Hello All,
When is nested class more preferable that Inheritance ? I think with proper
inheritance and friend class concept we can get the same flexibility as nested
classes
Any comments ..
Best Regards
KInd


It's an endlessly arguable topic.

I typically prefer nesting classes. A class heirarchy where the
subclasses depend on the implementation details of the superclass can
become inflexible.Forcing yourself to use the interface of the
contained class keeps the design loosely-coupled. Polymorphism can be
implemented using interfaces, which also tends to provide clearer
code, I find.

OTOH, if you absolutely need access to the innards of the superclass,
and the subclass would inevitably have to change if the design of the
superclass did then inheritance is obviously the way to go.

Others will no-doubt differ...

Jam
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Stephane Ninin | last post: by
10 posts views Thread by Paul Morrow | last post: by
11 posts views Thread by C# Learner | last post: by
3 posts views Thread by Martin Skou | last post: by
2 posts views Thread by miked | last post: by
18 posts views Thread by desktop | last post: by
9 posts views Thread by stephen.diverdi | last post: by
reply views Thread by leo001 | 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.