Re: Problem overriding >> and << in a template class
From: Dietmar Kuehl (dietmar_kuehl_at_yahoo.com)
Date: 03/03/05
- Next message: Dietmar Kuehl: "Re: multiple files"
- Previous message: Martijn Mulder: "Re: How to disable warning C4786 in VC++ 6.0?"
- In reply to: franklini_at_hotmail.com: "Problem overriding >> and << in a template class"
- Next in thread: franklini_at_hotmail.com: "Re: Problem overriding >> and << in a template class"
- Reply: franklini_at_hotmail.com: "Re: Problem overriding >> and << in a template class"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 3 Mar 2005 05:25:54 -0800
franklini@hotmail.com wrote:
> hello people i. can anybody help me,
Interestingly, this looks very much like an article I recently rejected
from comp.lang.c++.moderated because it uses excessive code, i.e. it
includes loads of stuff entirely irrelevant to the question. Of course,
the rejection also mentioned the cause the of problem: The functions
you declare friends within the body of the class are non-template
functions and, except for the same name, entirely unrelated to the
function templates you use later!
For the friend functions to work correctly you need to declare them
*prior* to the class definition which in turn means that you also need
to forward declare the class definition itself. In addition, you need
to use a template argument list in the friend declaration. However,
since the template arguments can actually be deduced from the function
arguments, this template argument list can be empty. It just has to be
present to indicate that the function template is made a friend. The
whole code would look something like this:
#include <iostream>
template <typename> class foo;
template <typename T>
std::ostream& operator<< (std::ostream&, foo<T> const&);
template <typename T>
struct foo
{
foo(): val(17) {}
// ...
friend std::ostream& operator<< <>(std::ostream&, foo<T> const&);
private:
int val;
};
template<typename T>
std::ostream& operator<< (std::ostream& os, foo<T> const& f)
{
return os << f.val;
}
int main()
{
std::cout << foo<int>() << "\n";
}
BTW, this is not "overriding" the output operator but "overloading".
There is signification difference between these two terms although
they look very similar. Overriding is the term used to indicate that
a virtual function of a base class is, well, overridden by a derived
class, i.e. it is the mechanism used to implement dynamic polymorphism.
The overridden function has essentially the same signature as the
original on (although the return type may be covariant; theoretically
the arguments could be contravariant but this is not supported by C++).
Overloading on the other means that the same function name is used for
an otherwise unrelated function: the signatures of overloaded functions
vary be definition and there is no dynamic polymorphism involved. Of
course, the unrelated functions should to similar things when they
share a common name but this is technically not required.
-- <mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/> <http://www.contendix.com> - Software Development & Consulting
- Next message: Dietmar Kuehl: "Re: multiple files"
- Previous message: Martijn Mulder: "Re: How to disable warning C4786 in VC++ 6.0?"
- In reply to: franklini_at_hotmail.com: "Problem overriding >> and << in a template class"
- Next in thread: franklini_at_hotmail.com: "Re: Problem overriding >> and << in a template class"
- Reply: franklini_at_hotmail.com: "Re: Problem overriding >> and << in a template class"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|