N
jesus was black schrieb:
Der typ der den Artikel geschrieben hat, hat keine ahnung.
Naja, er schliesst aufgrund von "erfahrenen" C++-Programmierern, die keine Punkt-Klasse schreiben können, darauf, dass C++ zwangsläufig kompliziert und zu gefährlich für grössere Projekte ist.
This really isn't a C++ issue. Failing to initialize pointers in C structs is equally bad. The new problems are: [...]
Testing for NULL before calling delete is unnecessary (since 'delete 0' is defined to be harmless), but causes no damage other than slowing down the program slightly.
Eindeutig ein schlimmes Problem, das ja in C mit free() ganz anders war.
NamedPoint now trashes the heap if any NamedPoint objects are passed by value (like, for example, returning a NamedPoint object from a function). This is because the copy constructor that C++ gives us for free copies the 'name' pointer, but does not copy the contents. Now, calling the destructor on the first shared 'name' returns the memory to the heap (although the second copy will continue to use it, EVEN IF THE MEMORY GETS ALLOCATED TO SOME OTHER USE). Calling the destructor on the second shared 'name' probably corrupts the heap by deleting memory that was not, at that time, allocated (the second delete isn't required to corrupt the heap, but this is how most C++ heap managers work).
It has similar problems with the default assignment operator.
Auch das kommt natürlich erst mit C++, da man in C viel ausgereiftere Kopiersemantiken hat und beliebig Objekte mit Zeigern per Value herumschieben kann, ohne jemals Probleme zu haben. Natürlich ist es auch realitätsnah, für jede kleine Klasse besitzende Zeiger hinzuzufügen und möglichst zu vergessen, sinnvolle Semantiken zu implementieren.
One solution is to do a lot more stuff with things like STL string objects and generally try to hide the heap allocation. The auto_ptr<> and similar classes help here, too. But they only help. The fundamental problem still remains -- it is too easy to write subtly wrong code and the language is littered with booby-traps.
Äh ja. Es ist einfach, Fehler zu machen, wenn man sichs einfach macht. Wenn man wirklich erfahren und nicht nur "erfahren" ist, hat man kaum noch Probleme, weil man seine Punkt-Klasse entweder mit std::string oder mit sinnvollen Memberfunktionen versieht. Aber es stimmt natürlich - solche Dinge muss man in C++ wirklich lernen, wobei man in anderen Programmiersprachen bereits vieles durch Herumprobieren und ohne grosses Hintergrundwissen anwenden kann. Aber ob das immer ein Vorteil ist...