>> |
07/01/11(Fri)01:19 No.3314058http://yosefk.com/c++fqa/class.html
[7.5] How does C++ help with the tradeoff of safety vs. usability?
FQA:
When you need multiple instances and encapsulation in C, you use a
forward declaration of a struct in the header file, and define it in the
implementation file. That's actually better encapsulation than C++
classes - there's still no run-time encapsulation (memory can be
accidentally/maliciously overwritten), but at least there's compile-time
encapsulation (you don't have to recompile the code using the interface
when you change the implementation).
The fact that a crude C
technique for approximating classes is better than the support for
classes built into the C++ language is really shameful. Apparently so
shameful that the FAQ had to distort the facts in an attempt to save
face (or else the readers would wonder whether there's any point to C++
classes at all). The FQA hereby declares that it will not go down this
path. Therefore, we have to mention this: the forward declaration
basically makes it impossible for the calling code to reserve space for
the object at compile time. This means that a struct declared in a
header file or a C++ class can sometimes be allocated more efficiently
than a forward-declared struct. However, this is really about a
different tradeoff - safety vs. efficiency, and there's no escape from
this tradeoff. Either the caller knows about the details such as the
size of an object at compile time - which breaks compile-time
encapsulation - or it doesn't, so it can't handle the allocation.
Anyway,
here's the real answer to the original question: C++ helps with the
tradeoff of safety vs. usability by eliminating both.
C++ is
extremely unsafe because every pointer can be used to modify every piece
of memory from any point in code. C++ is extremely unusable due to
cryptic syntax, incomprehensible semantics and endless rebuild cycles.
Where's your tradeoff now, silly C programmers? |