By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,244 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,244 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
On 7 Jul 2004 13:19:54 -0700, Blue Ocean <bl*********@hotmail.com> wrote:
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.
Jul 22 '05 #3

P: n/a
Something that calls itself Blue Ocean wrote:
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.
Jul 22 '05 #4

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cc**********@nntp1.jpl.nasa.gov>...
[snip]


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.
Jul 22 '05 #5

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsasbi8db212331@andronicus>...
On 7 Jul 2004 13:19:54 -0700, Blue Ocean <bl*********@hotmail.com> wrote:

[snip]


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.
Jul 22 '05 #6

P: n/a
Blue Ocean wrote:
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 (bl*********@hotmail.com).
Your Java and C++ comparison appears intended to incite a language war.
There is *good* reason to suspect that you are trolling.
Jul 22 '05 #7

P: n/a
On 7 Jul 2004 19:39:13 -0700, ag***********@gmail.com (Blue Ocean)
wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsasbi8db212331@andronicus>...
On 7 Jul 2004 13:19:54 -0700, Blue Ocean <bl*********@hotmail.com> wrote:

[snip]


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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #8

P: n/a
On 7 Jul 2004 19:30:31 -0700, ag***********@gmail.com (Blue Ocean)
wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cc**********@nntp1.jpl.nasa.gov>...
[snip]


Troll alert? Don't you think that's a bit harsh?


Don't worry, his nickname is "Trollsdale"; random "Troll Alert"
postings are his speciality.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cc**********@nntp1.jpl.nasa.gov>...
Blue Ocean wrote:

You use a troll handle (Blue Ocean) instead of your given name.
You post from a disposable email account (bl*********@hotmail.com).
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.
Jul 22 '05 #10

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsasbi8db212331@andronicus>...
[snip]
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
Jul 22 '05 #11

P: n/a
Blue Ocean wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cc**********@nntp1.jpl.nasa.gov>...
Blue Ocean wrote:

You use a troll handle (Blue Ocean) instead of your given name.
You post from a disposable email account (bl*********@hotmail.com).
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.


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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #12

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsasbi8db212331@andronicus>...
On 7 Jul 2004 13:19:54 -0700, Blue Ocean <bl*********@hotmail.com> wrote:
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.


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.
--
Imanpreet Singh Arora
isingh AT acm DOT org
Jul 22 '05 #13

P: n/a
ag***********@gmail.com (Blue Ocean) wrote in message news:<af**************************@posting.google. com>...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<opsasbi8db212331@andronicus>...
On 7 Jul 2004 13:19:54 -0700, Blue Ocean <bl*********@hotmail.com> wrote:

[snip]


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
Jul 22 '05 #14

P: n/a
Peter van Merkerk <me*****@deadspam.com> wrote in message news:<2l************@uni-berlin.de>...
Blue Ocean wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cc**********@nntp1.jpl.nasa.gov>...


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]
Jul 22 '05 #15

P: n/a

"Minti" <mi************@yahoo.com> wrote:
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
Jul 22 '05 #16

P: n/a
In message <e8**************************@posting.google.com >, Minti
<mi************@yahoo.com> writes
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.

--
Richard Herring
Jul 22 '05 #17

P: n/a
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:rb**************@baesystems.com...
In message <e8**************************@posting.google.com >, Minti
<mi************@yahoo.com> writes
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?

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++.
--
Gary
Jul 22 '05 #18

P: n/a
bl*********@hotmail.com (Blue Ocean) wrote in message news:<33**************************@posting.google. com>...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #19

P: n/a
"Gary Labowitz" <gl*******@comcast.net> wrote in message news:<5K********************@comcast.com>...
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:rb**************@baesystems.com...
In message <e8**************************@posting.google.com >, Minti
<mi************@yahoo.com> writes
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?

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?

--
Imanpreet Singh Arora "In order to paint 6 things are required:
i"kaur" #AT# acm #DOT# org spirit, rhythm, thought, scenery, pen and ink"
mv kaur singh
Jul 22 '05 #20

P: n/a
Minti wrote:

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().

Jul 22 '05 #21

P: n/a
bl*********@hotmail.com (Blue Ocean) wrote in message news:<33**************************@posting.google. com>...
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
Jul 22 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.