473,387 Members | 1,771 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,387 software developers and data experts.

pointer to function as template parameter

I have a

template<class T, class TBase>
TBase* MyFunc(int *a, int *b)
{
T* pT = new T;
return static_cast<TBase*>(pT);
}

(the a and the b are used in some way in the true function)

(clearly TBase must be a base class of T to make it work)

I want to create something like

list<pointerfunctions> a;

How do I write the signature of the function? I can't use typedef because
there is a template...

--- Thanks
May 28 '06 #1
3 1711
MaxMax posted:

I want to create something like

list<pointerfunctions> a;

How do I write the signature of the function? I can't use typedef
because there is a template...

Please elaborate on the restriction -- I don't see any reason why you
couldn't use typedef.
list< void (*)() > a;
/* Or alternatively */

typedef void (*SimpleFunc)();

list< SimpleFunc > b;

-Tomás
May 28 '06 #2

"MaxMax" <no**@none.com> wrote in message
news:%U********************@twister1.libero.it...
I have a

template<class T, class TBase>
TBase* MyFunc(int *a, int *b)
{
T* pT = new T;
return static_cast<TBase*>(pT);
Why the cast? pT is already a TBase*. And isn't this design likely to result
in memory leaks? This is mysterious stuff.
}
If you write

int a = 7;
int b = 12;
TBase *p = MyFunc(&a, &b);

how is the compiler supposed to figure out the type of T or TBase? (The type
of the return result cannot be used.)

I think a template class may be what you need but it is hard to be sure from
what you have given us.

(the a and the b are used in some way in the true function)

(clearly TBase must be a base class of T to make it work)

I want to create something like

list<pointerfunctions> a;
If you have a list of objects, they must all be of the same type. However,
they can be pointers (better yet smart pointers) to a common base class.
Again, this suggests you really want a polymorphic class rather than a
function here.

How do I write the signature of the function? I can't use typedef because
there is a template...

--- Thanks

May 28 '06 #3
> Please elaborate on the restriction -- I don't see any reason why you
couldn't use typedef.
list< void (*)() > a;
/* Or alternatively */

typedef void (*SimpleFunc)();

list< SimpleFunc > b;

Thanks a lot! I was finally able to do it! I had problems because I didn't
remember how the () and the (*) needed to be used.

--- bye
May 28 '06 #4

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

Similar topics

3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
15
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
7
by: agitlin | last post by:
Hello All, I'm working with a SDK for a piece of hardware attached to a Windows CE device. The sample code provided is all written in eVC++ and I'm trying to port it over to a VB.NET...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.