* baumann@pan:
i couldn't understand why code 1) can solve the problem of code 2) has.
code 1)
template<class T> T* clone(const T* tp)
{
return tp->clone();
}
and change our make_unique member to call it
template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? clone(p): 0; // call the global (not member) version
of clone
}
}
code 2)
template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? p->clone() : 0; // here is the problem
}
}
Look at the call to p->clone. Because we are using a Ptr< vector<char>, this call will try to call the clone function that is a member of vector<char>. Unfortunately, no such function exists!
template<class T> T* clone(const T* tp)
{
return tp->clone();
}
i think tp-clone() would has the same sematics of p->clone() since both
p and tp are of same type.
Correct.
so I couldn't understand the difference. how it can solve the problem
of calls undefined member function?
The global template function can be specialized for the type T in
question, e.g.
typedef std::vector<char> CharVector;
template<> CharVector* clone( CharVector const* p )
{
return new CharVector( *p );
}
But I don't have the book and haven't read it, so it's impossible for
me to know whether I've understood correctly what the "problem" is, and
if so, whether the above was the authors' intention.
I think of such things as design: what mechanism, if any, should be used to
support non-intrusive cloning? The "problem" is then to choose between e.g.
a template function, as above, or an interface, or whatever. That's more of
a real "problem" because the solution space is infinite, and experience
says that your chances of first choosing an UnGood solution are 99.5%...
Another real "problem" is to know when to stop generalizing a solution and
adapting it for future reuse that maybe never will occur.
Much of that work _will_ be for nothing, and some of it may even be counter-
productive (e.g. higher complexity, lower flexibility) and the "problem" is
to strike a balance so that the work that does pay off pays enough to cover
also for the work that doesn't.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?