sjelkjd, here's a reply for you. Oh, and offtopic is allowed here ;)

Started by
10 comments, last by civguy 21 years ago
Magmai, the evil moderator, decided to close the "C++ or C#"-topic for no one''s benefit. Since your (sjelkjd) email isn''t visible, I had to make a new topic for a reply. I hope the moderators are happy.
quote:Original post by sjelkjd How would you implement sort in java? It has to take two iterators, and sort the range with quicksort. Maybe I''m wrong here, but I thought you couldn''t swap the elements of two iterators in java.
You''re right, I really hadn''t thought it out throughoutly. In Java, you''d need some kind of assign(Object o)-method for the Object class to accomplish it. Now the iterator class can''t affect the objects it points at, since none of the Object''s methods change the object''s contents.
quote:But with java, you can only do things that the iterator interface supports.[...]
Your Addable-argument is a good argument for why Java''s system doesn''t work. But it''s among the only arguments: C++ template algorithms benefit from the fact that C++ has some conventions. + is used for adding, copy constructor is used for copying. So templates can easily exploit these facts and hope to get good results for many kinds of objects. But suppose you were to make a template algorithm that wants a vector class that supports taking length of the vector. Will the function be !vec, ~vec, vec.length() or vec.magnitude()? As soon as you stop using trivial operators, you''ll fall to the same level as Java programmers: You''ll have to implement the correct interface. In Java it requires you to use "implement", in C++ it requires you to provide function with a conforming signature.
quote:But, if you want to use it with other code, you have to write a wrapper around other people''s code to implement that interface.
Wrappers aren''t so uncommon in C++ algorithms either, due to the very same reason. std::less is a wrapper that uses the operator<() -interface. std::more is a wrapper that uses the operator>() -interface.
Advertisement
quote:
But suppose you were to make a template algorithm that wants a vector class that supports taking length of the vector. Will the function be !vec, ~vec, vec.length() or vec.magnitude()? As soon as you stop using trivial operators, you''ll fall to the same level as Java programmers: You''ll have to implement the correct interface. In Java it requires you to use "implement", in C++ it requires you to provide function with a conforming signature.

No no no, you using a more generalized notion of "interface" than what was meant. With regard to OO, when you say interface you mean an abstract base class with pure virtual methods. The "interface" with templates (or real functional programming) is parametric, essentially meaning it behaves polymorphically, but does not require a common point of derivation to do so.
The C++ template terminology in use to distinquish this from an OO interface, is "concept".

The C++ std::less wrapper is resolved at compile time and no trace of it can be found in non-debug builds. In Java we have to not only dereference a jump-table, but also jump to somewhere else in the code. The overhead ranges from 25% to 600% in compiled code (C89 is slower than C++98 at sorting for this reason).

...
This is what you''re suppose to do, the orginal poster in the last thread got frustrated and left since people were talking about everything except what you wanted to know about. I know I''ve been a pita lately, but it seriously detracts from the quality of the board if people go off on long rants when you''re trying to get an answer or advice, and it''s easy to make a new thread.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Magmai: sorry to have derailed it. It wasn''t as off-topic as you think though, since the proposal for java generics and c# generics are surprisingly similar. Hence, most of my arguments as to why c++ templates are superior to java generics apply to c# generics as well.

civguy:
A lot of it results from how memory is managed. c++ is much more direct and explicit, and all objects support a copy constructor. I know in c#, object doesn''t have a clone function - you have to implement ICloneable. I''m assuming it''s similar in java(last time I wrote a java class I didn''t have to implement Cloneable). By default, Object.clone() does shallow copy...which may or may not be correct.

I see your point about c++ having to specify an "interface"/"concept." However, the natural semantics of the language go pretty far here(operator[],operator(),operator=,copy ctr,operator==).

You can write a generic algorithm even you can''t change the classes it''s supposed to run on, because you can support arbitrary operations in your templated code, while if you''re writing java/c# generics you need an interface defined - and you don''t get any built in operators for free.
what do you think about the war in iraq?
My president can beat up your president.

James Simmons
MindEngine Development
http://www.mindgamez.com/me/
quote:Original post by Magmai Kai Holmlor
I know I''ve been a pita lately...


"pita" is a type of bread. "puta" is spanish for whore. It wouldn''t make sense to call yourself a pita, so I assume I am correct to correct you.

The word you were looking for was "puta."

-flan
the tasty custard dessert.
PITA = Pain In The Ass.

That resolved, can we get back on topic now?

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
"With regard to OO, when you say interface you mean an abstract base class with pure virtual methods."

Abstract base classes with pure virtual methods may be used to define an interface certainly, but the definition of an interface should not be restricted to this. Stroustrup frequently refers to an interface as those public methods of a class that enable a client to access the private data members of that class. He makes no mention of a class having to be derived from an abstract base.



ai-junkie.com
quote:Original post by flangazor
"pita" is a type of bread.


And it is the present conjugation of the verb Pitar for the third pronom in spanish. Pitar is what you do when you smoke, specifically when you suck the smoke.

He pita

On the other hand, Pito is the conjugation for the first pronom, and it is also a word for penis.

Suck my pito.

Interesting isn't?

[edited by - xaxa on April 1, 2003 6:53:34 AM]
[size="2"]I like the Walrus best.
quote:Original post by fup
Abstract base classes with pure virtual methods may be used to define an interface certainly, but the definition of an interface should not be restricted to this.

Absolutely. This is what happens when people think OO was invented by Sun's marketing department. Abstract base classes are really there to enforce the IS-A relationship at compile-time in statically-typed languages. Just because Java and COM decided to call this concept an "interface" does not mean it is the OO definition of an interface.
quote:
Stroustrup frequently refers to an interface as those public methods of a class that enable a client to access the private data members of that class.

Also check out Herb Sutter's Interface Principle. Non-members are fair game too.

[edited by - SabreMan on April 1, 2003 7:24:52 AM]

This topic is closed to new replies.

Advertisement