471,337 Members | 859 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Final and Cieled and nothing in C++

Has the fact that both Java and C# are garbage collected, and C++ in not,
anything to do with the fact that there is no language item to prevent a
class from being inherired from? I once read that Java and C# implement this
feature for preformance, but the C++ creators said it was not worse the
effort. So because Java and C# are garbage collected, in their case is it
worse the effort? What is the connection?
Feb 5 '06 #1
14 1597
My4thPersonality wrote:
Has the fact that both Java and C# are garbage collected,
and C++ in not, anything to do with the fact that there
is no language item to prevent a class from being
inherired from? I once read that Java and C# implement
this feature for preformance, but the C++ creators said
it was not worse the effort. So because Java and C# are
garbage collected, in their case is it worse the effort?
What is the connection?


"Why doesn't C++ have garbage collection?"
http://www.research.att.com/~bs/bs_f...age-collection
Feb 5 '06 #2

"My4thPersonality" <so*******@out.there> skrev i meddelandet
news:u2*************@TK2MSFTNGP10.phx.gbl...
Has the fact that both Java and C# are garbage collected, and C++ in
not, anything to do with the fact that there is no language item to
prevent a class from being inherired from? I once read that Java and
C# implement this feature for preformance, but the C++ creators said
it was not worse the effort. So because Java and C# are garbage
collected, in their case is it worse the effort? What is the
connection?


It is not about garbage collection, it is about vitual functions. In
C++, a function isn't virtual, unless you specifically make it so.
That pretty much removes the need to specify 'final' to make the
functions non-virtual again - just don't make it virtual in the first
place!

Also, you can prevent a C++ class from being inherited from, but
making its constructor private. This isn't used very much though. The
usual way is to add a note in the documentation, "This class isn't
designed for inheritance".
Bo Persson
Feb 5 '06 #3
Bo Persson wrote:
"My4thPersonality" <so*******@out.there> skrev i meddelandet
news:u2*************@TK2MSFTNGP10.phx.gbl...
Has the fact that both Java and C# are garbage collected, and C++ in
not, anything to do with the fact that there is no language item to
prevent a class from being inherired from? I once read that Java and
C# implement this feature for preformance, but the C++ creators said
it was not worse the effort. So because Java and C# are garbage
collected, in their case is it worse the effort? What is the
connection?


It is not about garbage collection, it is about vitual functions. In
C++, a function isn't virtual, unless you specifically make it so.
That pretty much removes the need to specify 'final' to make the
functions non-virtual again - just don't make it virtual in the first
place!

Also, you can prevent a C++ class from being inherited from, but
making its constructor private. This isn't used very much though. The
usual way is to add a note in the documentation, "This class isn't
designed for inheritance".


That said, final/sealed would be useful in C++ as an optimization, since
they allow the compiler to easily determine the final overrider of a virtual
function in some useful cases without doing whole program analysis. I think
that many C++ programmers would welcome the ability to mark a class as "not
inheritable". It's a common enough situation, and the idioms used to
prevent it in C++ are hacks at best, not proper language features. Easy to
get along without, yes, but still nice to have.

-cd
Feb 5 '06 #4
> In C++, a function isn't virtual, unless you specifically make it so.

And in Java and C# a function is virtual by default?

Feb 6 '06 #5
<ma*********@hotmail.com> wrote:
And in Java and C# a function is virtual by default?


Yes
Feb 6 '06 #6
ma*********@hotmail.com wrote:
In C++, a function isn't virtual, unless you specifically make it so.


And in Java and C# a function is virtual by default?


In Java it is. In C# it isn't.

Jon

Feb 6 '06 #7
And in Java and C# a function is virtual by default?

Jon
In Java it is. In C# it isn't.


Mmm...

Do both languages, Java and C#, actually have functions that are not
members of a class? They have not have they?

Feb 7 '06 #8
marcwent...@hotmail.com wrote:
In Java it is. In C# it isn't.


Mmm...

Do both languages, Java and C#, actually have functions that are not
members of a class? They have not have they?


Assuming you mean "methods which are not members of a type" - no. (I'm
being picky - "function" isn't a C# term, but structs *can* have
methods. I suspect we're on the same wavelength, but it's worth
clarifying just for the sake of posterity :)

Personally I wish that classes were sealed by default in C#, but then I
view inheritance as something to be used sparingly. (It's wonderful
where it *is* useful.)

Jon

Feb 7 '06 #9
Alex Blekhman:
http://www.research.att.com/~bs/bs_f...age-collection


More relevant would be this then:

"Why doesn't C++ have a final keyword?"

http://public.research.att.com/~bs/bs_faq2.html

But then, why would the lack of performance gain be relevant in Java,
but not relevant in C++.

Feb 7 '06 #10
ma*********@hotmail.com wrote:
Alex Blekhman:
http://www.research.att.com/~bs/bs_f...age-collection
More relevant would be this then:

"Why doesn't C++ have a final keyword?"

http://public.research.att.com/~bs/bs_faq2.html


Agree.
But then, why would the lack of performance gain be
relevant in Java, but not relevant in C++.


Sorry, I couldn't understand what you wanted to say.
Feb 7 '06 #11
> Sorry, I couldn't understand what you wanted to say.

Euh, well, I understand, follow the explination of Bjarne Stroustrup
that final has too little use to implement because the performance gain
in C++ would be little. But then I ask myself, why would the
performance gain in Java would be substantional.

"In C++, virtual function calls are so fast that their real-world use
for a class designed with virtual functions does not to produce
measurable run-time overheads compared to alternative solutions using
ordinary function calls."

Hence somehow, virtual functions in Java are slow and finalized
function a lot faster? Or it just a mre or less coincidental choice the
developers of both language decided to do and not do. By the way, I am
more in C++ then in Java and or C#, but I am studying the latter two
languages to broaden my knowledge.

Feb 7 '06 #12
ma*********@hotmail.com wrote:
Sorry, I couldn't understand what you wanted to say.
Euh, well, I understand, follow the explination of Bjarne
Stroustrup that final has too little use to implement
because the performance gain in C++ would be little.


No, B.Stroustrup mentions two common arguments of `final'
proponents. One is possible performance gain (when callin
virtual functions), second is safety (to prevent unwanted
derivation).
Hence somehow, virtual functions in Java are slow and
finalized function a lot faster? Or it just a mre or less
coincidental choice the developers of both language
decided to do and not do.


I don't think that `final' concept exists in Java solely
because of performance considerations. Moreover, I reckon
that performance has very little part in `final' argument.
It exists to provide class hierarchy designers with means to
ensure logic correctness. By applying `final' specifier
function doesn't cease to be "virtual" (as we understand it
in C++). It merely prevents farther derivations/overloading.
"Virtuality" still works until it finds final derivative.
Feb 7 '06 #13

<ma*********@hotmail.com> skrev i meddelandet
news:11**********************@g14g2000cwa.googlegr oups.com...
Sorry, I couldn't understand what you wanted to say.
Euh, well, I understand, follow the explination of Bjarne Stroustrup
that final has too little use to implement because the performance
gain
in C++ would be little. But then I ask myself, why would the
performance gain in Java would be substantional.


Because in C++ functions are not virtual by default, so the use for
final is limited.

"In C++, virtual function calls are so fast that their real-world
use
for a class designed with virtual functions does not to produce
measurable run-time overheads compared to alternative solutions
using
ordinary function calls."
On C++, a function is virtual only if explicitly made so. That
probably is part of the interface design for subclasses, so there is s
genuine need for the function to be virtual.

Again, the few exceptions from this doesn't make it worthwhile to
introduce a new concept. A small gain in very few places. Is it worth
it?

Hence somehow, virtual functions in Java are slow and finalized
function a lot faster? Or it just a mre or less coincidental choice
the
developers of both language decided to do and not do. By the way, I
am
more in C++ then in Java and or C#, but I am studying the latter two
languages to broaden my knowledge.


There are a lot more vitual functions in Java, so there are more
opportunities for finalizing functions. I also suspect that early Java
systems didn't optimize as well as C++ compilers can do. Perhaps the
gain used to be greater?
Bo Persson
Feb 7 '06 #14
Alex:
Because in C++ functions are not virtual by default, so the use for
final is limited.


Ok, so in C++ I just do not declare a function as virtual, while in
Java I explicitly declare it final because it is java-virtual by
default. Hence considering functionality I already got an alternative
in C++.

I thank you and Bo, and the others for the explination. It's clearer
now.

Feb 8 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Anthony Martin | last post: by
reply views Thread by Gary Broughton | last post: by
10 posts views Thread by Bezalel Bareli | last post: by
reply views Thread by Geir Baardsen | last post: by
5 posts views Thread by Anthony Baxter | last post: by
14 posts views Thread by Rahul | last post: by
5 posts views Thread by kiran | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.