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

B

Blue Ocean

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?
 
E

E. Robert Tisdale

Something said:
The reason why I ask is because
I am unfamiliar with the idea of templates.

The obvious solution is to learn something about 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.

It *can* be done with "some generic superclass"
*besides* using templates!
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?

Class templates are preferred because
compile time optimizations can be applied to templates classes
that cannot be applied to classes with virtual functions.
 
A

Alf P. Steinbach

* Blue Ocean:
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?

Templates give static typechecking (at compile+link time).

A common base class gives dynamic typechecking (at run-time).

Static typechecking is superior for producing correct code, _and_
is generally also superior with respect to performance.

One downside is that templates in C++ are not well integrated with
the rest of the language, and are so complex that most compilers
don't support them fully, which in practice means that if you
intend to produce portable code it's not enough to follow the
Holy Standard; the code must tested with and adapted to the various
compilers of interest, and perhaps even be non-standard-conforming.

Another downside is that template code is generally very difficult to
debug and to maintain, so the idea is to code only very general and very
simple things as templates.
 
J

John Harrison

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.

With a generic super class you have to downcast when you retrieve the
object from the Stack. Doing that is either inefficent (if you use
dynamic_cast) or not type safe (if you use static_cast). Either way its
ugly. So I've always thought that this is a weakness of Java.

How many times have you written an application where you wanted to put
anything, literally objects of any type, onto a Stack. Almost always you
have one type or some small set of types in mind, this is what C++ lets
you do.

Why do you think Java's way is easier? I've always thought that they were
forced into generic super classes precisely because they didn't have
templates.
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?

I'm sure it was thought of and rejected. Bjarne Stroupstrup was certainly
familar with Smalltalk, which has a generic superclass just like Java.
 
B

Blue Ocean

E. Robert Tisdale said:

Troll alert? Don't you think that's a bit harsh? Pardon a newbie for
offending your sensibilities, but please don't jump to conclusions.
My experience is limited, so I came here for advice, not to be
lambasted by someone who can't deal with even a perceived (not real)
challenge to their opinions.

If my wording was a bit off, I'm sorry, but the way you responded was
totally uncalled for. I like C++ better than Java, and I'm not here
to put it down. If I was, I'd think of better ways of doing it than
pretending to be a newbie.
 
B

Blue Ocean

John Harrison said:

Thanks for the help. I guess that what the other guy said about how
static type-checking is superior in general is true. Thinking about
it, I can't think of any case in which I would have put objects of two
different types into a collection last semester when I was using Java,
so static type checking would not have made a difference. And if the
performance is better, all the more reason to use it.
 
E

E. Robert Tisdale

Blue said:
Troll alert? Don't you think that's a bit harsh?
Pardon a newbie for offending your sensibilities,
but please don't jump to conclusions.
My experience is limited, so I came here for advice,
not to be lambasted by someone
who can't deal with even a perceived (not real)
challenge to their opinions.
If my wording was a bit off, I'm sorry,
but the way you responded was totally uncalled for.

It certainly was called for.
I like C++ better than Java, and I'm not here to put it down.
If I was, I'd think of better ways of doing it
than pretending to be a newbie.

You use a troll handle (Blue Ocean) instead of your given name.
You post from a disposable email account ([email protected]).
Your Java and C++ comparison appears intended to incite a language war.
There is *good* reason to suspect that you are trolling.
 
T

tom_usenet

John Harrison said:

Thanks for the help. I guess that what the other guy said about how
static type-checking is superior in general is true. Thinking about
it, I can't think of any case in which I would have put objects of two
different types into a collection last semester when I was using Java,
so static type checking would not have made a difference. And if the
performance is better, all the more reason to use it.

Java 1.5 (due soon) adds generics to Java, using an implementation
that allows typesafe containers, but doesn't give you a performance
improvement. Better than nothing I suppose.

Tom
 
B

Blue Ocean

E. Robert Tisdale said:
Blue Ocean wrote:

You use a troll handle (Blue Ocean) instead of your given name.
You post from a disposable email account ([email protected]).
Your Java and C++ comparison appears intended to incite a language war.
There is *good* reason to suspect that you are trolling.

My given name is James F. Aguilar. I've been trying to get Google to
show it for some time but even if I look, I can't find the place to
change what it displays. Now that I'm using a gmail account, please
understand that this is by no means a disposable e-mail address, since
they are by invite only and come with a lot of nice features that
hotmail doesn't have.

I will attempt to avoid posting Java/C++ comparisons in the future,
unless I absolutely must in order to obtain understanding.
 
P

puppet_sock

John Harrison said:
How many times have you written an application where you wanted to put
anything, literally objects of any type, onto a Stack. Almost always you
have one type or some small set of types in mind, this is what C++ lets
you do.

Well, with your question as written, the answer is exactly zero.

I've contemplated cases where it would be nice to have a fairly
hefty set of types to go on the stack, and that the set could
easily be expanded. One such example would be implementing a
postscript display engine. There you have a stack driven system
where you can shove things like tokens, sub-programs, dictionaries,
fonts, arrays, etc., onto the stack. This can, of course, be done
fairly easily in C++, in several ways. One way is to implement
something that mimics some of the features of the Java "object"
class. Another would be some variant on smart pointers, and some
kind of heirarchy. There are, of course, other ways.
Socks
 
P

Peter van Merkerk

Blue said:
My given name is James F. Aguilar. I've been trying to get Google to
show it for some time but even if I look, I can't find the place to
change what it displays. Now that I'm using a gmail account, please
understand that this is by no means a disposable e-mail address, since
they are by invite only and come with a lot of nice features that
hotmail doesn't have.

In that case I hope a very good spam filter is one of them. If you put
your real e-mail address on newsgroups spammers will find you. By the
way, your original posting was made from a hotmail account. Combined
that with a posting with "Java" in the subject line and you are likely
to trigger our overly sensitive troll detector (E. Robert Tisdale).
I will attempt to avoid posting Java/C++ comparisons in the future,
unless I absolutely must in order to obtain understanding.

You did nothing wrong. But every now and then some people try to provoke
language wars so the allergic response of Mr. Tisdale may be
understandable (but not justified). It is probably a good idea to avoid
the Java in the subject line.
 
M

Minti

John Harrison said:
With a generic super class you have to downcast when you retrieve the
object from the Stack. Doing that is either inefficent (if you use
dynamic_cast) or not type safe (if you use static_cast). Either way its
ugly. So I've always thought that this is a weakness of Java.

How many times have you written an application where you wanted to put
anything, literally objects of any type, onto a Stack. Almost always you
have one type or some small set of types in mind, this is what C++ lets
you do.

Why do you think Java's way is easier? I've always thought that they were
forced into generic super classes precisely because they didn't have
templates.


I'm sure it was thought of and rejected. Bjarne Stroupstrup was certainly
familar with Smalltalk, which has a generic superclass just like Java.

Well, that makes me wonder what if both could be provided
simultaneously. AFAIK JDK 1.5 has support for templates. It would be
really _cool_ and _helpfull_ if C++ could bring in the concept of
Superclass. Or is it toooo late for it. OK IMO we would get linker
errors when compiling files which use the Object code compiled with no
superclass but isn't it worthed the price.
 
A

Arijit

John Harrison said:

Thanks for the help. I guess that what the other guy said about how
static type-checking is superior in general is true. Thinking about
it, I can't think of any case in which I would have put objects of two
different types into a collection last semester when I was using Java,
so static type checking would not have made a difference. And if the
performance is better, all the more reason to use it.

One important reason(I think I read this in Stroustrup's website,
I don't remember) is that C++, unlike Java, supports multiple
inheritance. So, while in Java, you either inherit from object
or from some other class(which in turn inherits from object),
in C++ you may have inherit from three classes, which inherit
from superclass, so you end up with three superclass bases. (It
will not happen if the superclass is automatically virtual) But
this is a just a minor point. The important point is that what
you are trying to achieve in the Stack example can be done with
multiple inheritance, without using any superclass. For example

class D
{
public:
virtual ~D();
};

class Stack
{
public:
void push(D*);
D* pop();
// etc
};


class MyClass : public D //, public (other classes,not possible in Java)
{ };

int main()
{
Stack S;
MyClass My;

S.push(&My);
MyClass *M = dynamic_cast<MyClass*>( S.pop() );

return 0;
}

As you see, no templates are needed. Any class that you want
to store in the Stack just has to inherit from D.

-Arijit
 
B

Blue Ocean

Peter van Merkerk said:
In that case I hope a very good spam filter is one of them. If you put
your real e-mail address on newsgroups spammers will find you. By the
way, your original posting was made from a hotmail account. Combined
that with a posting with "Java" in the subject line and you are likely
to trigger our overly sensitive troll detector (E. Robert Tisdale).

Interesting story: I posted somewhat regularly on
rec.games.roguelike.nethack for about two months with my hotmail
account ~1 year ago, and I still never get spam on it. In any case,
the google e-mail is probably going to stay as a just for news e-mail
address because I really like my old hotmail name; it's been with me
for a long time. I'll take your advice about Java in the subject
line.

[OT] Scarcely more than a day after I got this google e-mail address
with 1 GB of storage and the ability to send 10 MB attachments, I get
an e-mail from hotmail that says they're increasing their inbox size
to 250 MB and giving me permission to send 10 meg attachments (still
free). That is totally hot capitalism for you right there. Power to
the people![/OT]
 
C

crichmon

Minti said:
Well, that makes me wonder what if both could be
provided simultaneously. AFAIK JDK 1.5 has support
for templates. It would be really _cool_ and
_helpfull_ if C++ could bring in the concept of
Superclass. Or is it toooo late for it. OK IMO we
would get linker errors when compiling files which
use the Object code compiled with no superclass but
isn't it worthed the price.

One could always implement a class Object and have any class that doesn't
inherit anything inherit from Object explicitly. Right...? :)

crichmon
 
R

Richard Herring

Minti said:
Well, that makes me wonder what if both could be provided
simultaneously. AFAIK JDK 1.5 has support for templates. It would be
really _cool_ and _helpfull_
Why?

if C++ could bring in the concept of
Superclass.

How would this differ from an ordinary class? If you want all your
classes to inherit from a single base class, there's nothing to stop you
defining them that way. But what does it buy you, except to help to
break type safety?
Or is it toooo late for it. OK IMO we would get linker
errors when compiling files which use the Object code compiled with no
superclass but isn't it worthed the price.

Failure to compile strikes me as a pretty high price to pay for any
innovation.
 
G

Gary Labowitz

Richard Herring said:
It allows for default functions of general use for all objects, such as a
toString function that converts an object to a printable self-descriptor, a
hashcode function that supplies a hash value for any object, etc. But C++
has simple way to include such general functions by using them and allowing
the linker to include them in the executable. There is no need for this kind
of Superclass in C++.
 
J

Jerry Coffin

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?

This was thought of and, in fact, implemented in most early class
libraries, such as the MFC, OWL, one from the NIH, etc. Performance,
while reduced somewhat, is the least of its problems.

Class libraries ultimately reflect the organization that produce them.
A single hierarchy with a single root is almost always the product of
a single organization that the rest of the world receives as more or
less a fait accompl (e.g. Xerox PARC/Smalltalk, Sun/Java, OWL/Borland,
MFC and .NET/Microsoft, the NIH class library, etc. ad naseum).

If you have a large number of more or less independent contributors,
that model becomes almost impossible to manage -- different people and
different purposes lead to different requirements on the base class.
In many cases, these requirements are mutually contradictory. Even
when they are not mutually contradictory, a single hierarchy almost by
definition increases coupling between all the classes involved, even
those that have little or no other reason to be related at all.

The result is that development of a single hierarchy requires a
relatively high degree of cooperation and coordination between all
parties doing any significant development related to it. If the
situation remains one of a single developer with others having far
smaller roles, this can be reasonable. When/if you have a large number
of contributors and (particularly) when the contributors start to
contribute more equally, this model becomes less manageable. When (as
in the case of C++ in general) you start out with a huge number of
people with conflicting ideas of what needs to be accomplished (to the
extent that even the language proper has become somewhat fragmented,
such as Embedded C++) there is essentially no possibility of producing
a single hierarchy that would be of any real use to anyone.
 
M

Minti

Gary Labowitz said:
It allows for default functions of general use for all objects, such as a
toString function that converts an object to a printable self-descriptor, a
hashcode function that supplies a hash value for any object, etc. But C++
has simple way to include such general functions by using them and allowing
the linker to include them in the executable. There is no need for this kind
of Superclass in C++.

Without a generic superclass how can you provide consistency?
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top