Test a new version today Code A bug is found because a class written by a colleague needs to use pushparam () before calling the execute () method on its instance () the method is to "Press" the parameter of the type const char * to this object. Class Foo {
Public :
Void Pushparam ( Const Char * PARAM );
Bool Execute ();
}; In terms of class behavior, I naturally think that pushparam () will save a copy of the string, because the common STL Algorithm Or containers do this, and it is safer and more important, no annotations explicitly require that "the specified string pointer must be valid for a long time (not before the foo object is destroyed)". In fact, for efficiency, the implementation of the foo class only saves the string pointer, not a copy. It assumes that this parameter is valid for a long time, the new Code pushes a temporary String object here, and the problem arises ...... in fact, there is no such problem. The key to the problem is:
- Sound comments are necessary
- Code security is superior to (so-called) Efficiency in most cases. The so-called "so-called" is used because of the overhead of saving a copy of a string in the speed and space of today's computers, it is really minimal.
- According to Delphi, if C ++ has a string with reference count, the efficiency and security are both considered. C ++ is quite convenient to implement string reference counting. The difference is that Delphi hides all this behind the compiler.