| re: Clarification related to template definition?
"Karthik D" <karthikd22@hotmail.com> wrote in message
news:e023a2c9.0310150944.764131ef@posting.google.c om...[color=blue]
> Hello All,
> I am a newbie to C++ code particularly templates.Below is a
> template class defintion and I couldn't find the ObjPtr/ObjFnPtr class
> definition anywhere in source code.[/color]
I suspect you're getting misled by this 'overloaded' use
of the keyword 'class'. Used with a template paramter
specifictation, the keyword doesn't mean 'class' as in
a user defined type. Here it means simply 'type'.
(The keyword 'typename' was added to the language for
this purpose as well (but 'class' was kept for back-
compatibiliy)).
The two types 'ObjPtr' and 'ObjFunPtr' will have the
actual types specified by an instantiation. Of course
the usage of these types within a templated class or
function can (and often does) impose restrictions and
requirements upon the specific argument types.
template<typename T>
class X
{
T member;
public:
X(const T& arg) : member(arg) { }
T foo() { return member % 10; }
};
int main()
{
X<int> x1(42); // makes 'T' above mean 'int'
cout << x1.foo() << '\n'; // OK
X<double> x2(3.14); // makes 'T' above mean 'double'
cout << x2.foo() << '\n'; // not OK, '%' invalid for 'double'
return 0;
}
[color=blue]
> template<class ObjPtr, class ObjFunPtr>
> class MemberFunctor0 : public Functor
> {
> public:
> MemberFunctor0(const ObjPtr& obj, const ObjFunPtr& objfn)
> : _obj(obj), _objfn(objfn)
> {}
>
> virtual void operator()()
> { ((*_obj).*_objfn)(); }
> private:
> ObjPtr _obj;
> ObjFunPtr _objfn;
> };
> Is this a way a template class could be defined?[/color]
Sure. (Barring any syntax errors, etc. I might have missed).
[color=blue]
>But what does
> ObjPtr/ObjFnPtr contains?[/color]
Whatever the actual types passed to those template parameters
are defined to be. As I mention above, any such types must
support any behavior which the template class using them
calls upon.
[color=blue]
> Could someone kindly clarify?
> Actually I wanted to print the function name of callback in
> the code which is contained in _objfn or so?[/color]
I'm not sure what you mean.
-Mike |