LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2006, 08:34 AM   #1
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
pointers to templated c++ functions


does anyone know how to create a typedef'd pointer to a function in c++ ?, I'm stumped.
 
Old 08-11-2006, 08:45 AM   #2
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Hi,
some sample code that worked for me:
Code:
#include <iostream>
using namespace std;

int foo(int a, int b)
{
        return a+b;
}

typedef int(*add_t)(int,int);


void bar(add_t func)
{
        cout<<func(1,1)<<endl;
}

int main()
{
        bar(foo);
        return 0;
}
exman
 
Old 08-11-2006, 09:13 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
int main()
{
        bar(foo);
        return 0;
}
The above is correct C code but not good C++ code, C++ should include the ampersand operator.
Code:
int main()
{
        bar(&foo);
        return 0;
}
 
Old 08-11-2006, 09:24 AM   #4
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Hi,

tried that and works too.
But both dont bring up a warning with
Code:
g++ -o td -Wall td.cpp -pedantic
Could you explain why one should use the &?
Im just interested.

exman
 
Old 08-11-2006, 09:37 AM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
hmm

i thought & was optional in both, same meaning with or without
 
Old 08-11-2006, 09:52 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by xhi
hmm

i thought & was optional in both, same meaning with or without
This is why I didn't say "none valid" or "bad" rather just "not good"
The ampersand is required for none static member functions and it's just good style to be consistant.
Sorry if I mislead you.
 
Old 08-11-2006, 10:06 AM   #7
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
yeah, shit, I forgot to mention that the functions were templated, kindof a big detail to forget. Anyways I now think that it isn't possible as I should be able to create pointers to instansiated functions but not to abstract templates of functions, could someone confirm or deny the please?, much obliged Gearoid
 
Old 08-11-2006, 10:35 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Can you give a small example to what you mean(member functions?), code speaks a thousand words
I'm also trying to think of a need for this?

Last edited by dmail; 08-11-2006 at 10:40 AM.
 
Old 08-11-2006, 11:00 AM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by dmail
This is why I didn't say "none valid" or "bad" rather just "not good"
The ampersand is required for none static member functions and it's just good style to be consistant.
Sorry if I mislead you.
yeh true. i think ive been coding in c for too long lately.
 
Old 08-11-2006, 12:08 PM   #10
orhun
LQ Newbie
 
Registered: Jun 2006
Distribution: Fedora 7
Posts: 11

Rep: Reputation: 0
I don't have access to gcc right now but this worked for me under VC80.
Code:
template<typename T>
void MyTemplatedFunction(const T& t)
{
	std::cout << t << std::endl;
}


typedef void (*FuncIntT)(const int&);
typedef void (*FuncStrT)(const std::string&);

int main(int argc, char* argv[])
{
	FuncIntT func1(&MyTemplatedFunction);
	func1(5);

	FuncStrT func2(&MyTemplatedFunction);
	func2("string");
	return 0;
}
 
Old 08-11-2006, 12:22 PM   #11
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Quote:
Originally Posted by gearoid_murphy
yeah,could someone confirm or deny the please?
I tried orhun's example with something like this
Code:
template<typename T>
typedef T (*foo)(T&,T&);
g++ -o tpp test.cpp
test.cpp:11: error: templatedeclaration of »typedef«
So its not possible or at least not that easy

exman
 
Old 08-11-2006, 01:12 PM   #12
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
could you give an example of what you are trying to accomplish? what purpose will these function pointers serve?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: Specialized templated member functions? spursrule Programming 1 07-01-2006 07:16 PM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 10:14 AM
C Programming: Help with Pointers to Functions devinWhalen Programming 2 03-04-2005 01:00 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration