Carl Youngblood wrote:
I imagine this subject has probably been brought up numerous times.
Forgive me for bringing it up again. I was googling through old posts
on this newsgroup about it and found a good suggestion on how to add
functionality to the string class. Rather than trying to inherit from
the string class, the author was suggesting wrapping the string class
in another class, like so:
Right !!!, we should not derive from string, but we should wrap the
string class, for example.
class Mystring {
string data;
Mystring(char* x) : data(x){};
Mystring(string* x) : data(x){};
Mystring& operator=(const Mystring& x){
data=x.data;
return *this.
}
}
The only confusing thing about his post was that he said this at the
end:
This is actually a very easy thing to do. You just have to check all
theinterface functions of string class (which is basic_string<char>) and
do thesame inline like above. The new class will have about the same
efficiency asthe std::string class one (because of the inlining), however, you
will beable to extend your own additions of behaviors. Don't forget to
define thevalue type, iterator type and reference type, like this ....
typename string::value_type value_type;
and etc .....
I don't know exactly what he meant by "Don't forget to define the
value type, iterator type and reference type". I've done a good bit
of C++ programming but am by no means an expert.
It's bad advice.
There are two kinds of classes: Library classes, and application-specific
classes.
Library classes must have extraordinarily wide interfaces, to ensure they
see use. Iterators and traits permit std::string to satisfy many many more
situations than any single application should ever use.
Application-specific classes must have the narrowest interfaces possible to
do their jobs. If an application-specific class delegates to a library
class, it should HIDE that super-wide interface, not reveal every stinkin'
permutation of it.
Do not ever add any method anywhere that nobody needs, right now. Prove
every method works with both unit tests and with re-use among other
application classes that are themselves unit-tested.
The more methods a class exposes, the less flexible that class's internals
become. If you needed to replace std::string with CString or _bstr_t (and
you might find legitimate reasons to do so), your change should be as simple
as replacing MyString's data, and then trivially re-writing each method.
When all of MyString's tests pass, integrate and run all integration tests,
and you are done.
That refactor would be incredibly difficult if you wrote every method
std::string uses, and if application code then called all of those methods.
--
Phlip
http://industrialxp.org/community/bi...UserInterfaces