Is there anything in C++ akin to Java's class Object?

R

red floyd

Minti said:
Without a generic superclass how can you provide consistency?


class A { ... } ;
class B { ... } ;
class C : public A, public B { ... };

What is the "generic superclass" of C?

If you need to access something from a parent class, you *KNOW* what
your parent is, and can use either A::member or B::member within your
C::method().
 
N

Nicholas Salerno

The reason why I ask is because I am unfamiliar with the idea of
templates. It seems like it would be easier if all classes that
needed something like

template<class T> class Stack { ... }

could be done with just some generic superclass, instead of using a
template.

If this is not the case, does the reason have something to do with
performance, or is it just an alternative that was not thought of?

As far as I know, from reading Stroustrup's books, he did not like the
concept of a single hierarchy with a single root class 'Object'. A
consequence of this design is that you can only inherit from one
class. However, you can still implement many interfaces. His
reasoning was that some classes will have a brittle relationship in
the hierarchy.

Having a generic super class can lead to trouble. First, as others
have already mentioned, you lose type safety. Although, I personally
have not found that to be a problem when I use Java or Delphi.
Second, and more importantly, someone may attempt to derive a
container class from the super container class. The Liskov
Substitution Principle will be violated.

Nicholas
 

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
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top