Clarification related to template definition?

K

Karthik D

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.
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?But what does
ObjPtr/ObjFnPtr contains?
Could someone kindly clarify?
Actually I wanted to print the function name of callback in
the code which is contained in _objfn or so?

Thanks & Regards,
Karthik
 
W

WW

Karthik said:
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.
template<class ObjPtr, class ObjFunPtr> [SNIP]
Is this a way a template class could be defined?
Yes

But what does ObjPtr/ObjFnPtr contains?

Nothing. Those are type parameters, much like parameters of a function.
They will only contain csomething, when the template is /instantiated/, with
concrete types as template arguments.
Could someone kindly clarify?

I think if you really want to understand this you will need a book or a
tutorial about C++ templates.
Actually I wanted to print the function name of callback in
the code which is contained in _objfn or so?

I do not understand this, but I guess you wanted to see the sources of the
template parameters. They can be anything (type), what the template can
use. One very simple (and stupid) example:

template <class T> struct Something {
T theThing;
};

#include <string>

int main() {
Something<int> i;
Something<long> l;
Something<double> d;
Something<std::string> s;
}

i.theThing is an int.
l.theThing is a long.
d.theThing is a double.
s.theThing is a string.
 
M

Mike Wahler

Karthik D said:
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.

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;
}

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?

Sure. (Barring any syntax errors, etc. I might have missed).

But what does
ObjPtr/ObjFnPtr contains?

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.

Could someone kindly clarify?
Actually I wanted to print the function name of callback in
the code which is contained in _objfn or so?

I'm not sure what you mean.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top