473,325 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Clarification related to template definition?

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
Jul 19 '05 #1
2 1513
WW
Karthik D wrote:
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.

--
WW aka Attila
Jul 19 '05 #2

"Karthik D" <ka********@hotmail.com> wrote in message
news:e0**************************@posting.google.c om...
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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
18
by: Stephen Waits | last post by:
Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For...
7
by: Rakesh | last post by:
Hi, I was writing this C++ program wherein I used an include statement like - #include <iostream.h> I was told by my co-worker that this form of including a file is deprecated and should...
5
by: ma740988 | last post by:
Consider a number of instances of template class vector<a>, vector<b> ..., vector<f> The types are - of course different. Of interest is an efficient way to access them at runtime. One...
2
by: sarathy | last post by:
Hi all, I have a clarification in C++ Templates. Consider that we have created a template as below template <class T1,class T2, class T3> void real_test(T1 a, T2 b ,T3 c) { const char *x=a;...
2
by: Philipp Reh | last post by:
Dear group, I recently tried to port some of my code to a VC++8.0 environment. I noticed that some boost::enable_if related code fails to compile there which works under gcc. I've made a...
9
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <vector> using namespace std; template<class Tclass Vec : public vector<T> {
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.