Nested Class vs Inheritance

K

KInd

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
 
C

Chris Theis

KInd said:
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 doesn´t 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
 
V

Victor Bazarov

KInd said:
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
 
H

Howard

KInd said:
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
 
J

James Moughan

KInd said:
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
 

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

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top