Connecting Tech Pros Worldwide Help | Site Map

Clarification related to template definition?

Karthik D
Guest
 
Posts: n/a
#1: Jul 19 '05
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
WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Clarification related to template definition?


Karthik D wrote:[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.
> template<class ObjPtr, class ObjFunPtr>[/color]
[SNIP][color=blue]
> Is this a way a template class could be defined?[/color]

Yes
[color=blue]
> But what does ObjPtr/ObjFnPtr contains?[/color]

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.
[color=blue]
> Could someone kindly clarify?[/color]

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

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.

--
WW aka Attila


Mike Wahler
Guest
 
Posts: n/a
#3: Jul 19 '05

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


Closed Thread