super superclass

M

Marc

In C++, is there a class like the Java Object class that all classes
derive from?

No.

Now depending on what you want to use Object for, there may still be
ways to accomplish it.
 
C

Christopher Pisz

In C++, is there a class like the Java Object class that all classes derive from?


I've seen a good number of people coming from java that decided to roll
their own in C++, and more times then not, it ends up causing problems.

Instead, decide what you need a particular class to do, or group of
classes. You are in the danger zone when you start thinking along the
lines of "All classes should do X."

For example, do all classes really need a ToString() method? Surely,
there are going to be some classes where a string representation either
makes no sense or has little value. However, it might make sense to have
some common method like ToXml() for all configuration classes....
 
J

Jorgen Grahn

I've seen a good number of people coming from java that decided to roll
their own in C++, and more times then not, it ends up causing problems.

That's what Stroustrup calls "don't use C++ as if it was Smalltalk",
and he's warned against it countless times.

(We don't know why the OP asked, though. He didn't say he wanted to
use such a design.)

....
For example, do all classes really need a ToString() method? Surely,
there are going to be some classes where a string representation either
makes no sense or has little value.

Unfortunate example, since the standard way to do this in C++ is to
provide a ostream << Foo overload. No common superclass needed.
However, it might make sense to have
some common method like ToXml() for all configuration classes....

/Jorgen
 
N

Nobody

Unfortunate example, since the standard way to do this in C++ is to
provide a ostream << Foo overload. No common superclass needed.

But that will dispatch according to the static (compile-time) type, not
the dynamic (run-time) type.
 
J

Jorgen Grahn

But that will dispatch according to the static (compile-time) type, not
the dynamic (run-time) type.

Yes, but is that a problem? It just has to be implemented differently
for those types:

std::eek:stream& operator<< (std::eek:stream& os, const SomeSuperClass& val)
{
return val.put(os);
}

/Jorgen
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top