Re: Static method vs. template function
Chris wrote:
Just curious about technicalities here, but is there technically a
difference between a static method that gets bound per class and a
template method that gets instantiated per type?
That is, if we have:
class A
{
public:
static void untemplateMethod(A const& object);
};
template<typename DataType>
void templateMethod(DataType const& object)
{
};
There is only one untemplateMethod for every A, and also if there is a
subclass of A, the subclasses get their own as well.
But, templateMethod gets instantiated only for each object that it
gets used for as well, so in a sense only one exists for each class
type as well.
Is this a misunderstanding? Is one preferred over the other?
There is one significant IMO difference: a member of the class can be
called using [a pointer or a reference to] an object, a static function
cannot... OK, there's another significant difference: the member
function has access to the private members of the class, the template,
even when instantiated, doesn't... OK, there's yet another diffenence:
you _have_ to use TYPE:: notation to get to the static member outside
of the class scope, and you _cannot_ use that for a non-member.
When it comes to calling a function from within another member of the
same class, say, using (*this) as the argument, there is no difference.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]