Sign in to follow this  
Axiverse

Function Pointer Woes [solved]

Recommended Posts

Axiverse    366
Ahh! 'Tis thy end for me. I wilt in thy face of function pointers... Umm, what is wrong with this...? It is horrid...
class Dele    // line 6
{
public:

	Dele(int (func*)()) : _function(func*)
	{

	}


	int (_function*)();
};
Error Messages:
...main.cpp(10) : error C2059: syntax error : 'function-style cast'
...main.cpp(10) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
...main.cpp(16) : error C2143: syntax error : missing ')' before '*'
...main.cpp(16) : error C2143: syntax error : missing ';' before '*'
...main.cpp(16) : error C2059: syntax error : ')'
...main.cpp(16) : error C2059: syntax error : ')'
...main.cpp(16) : error C2238: unexpected token(s) 
So frustrating are function pointers... But not as frustrating as function pointers + templates =( [Edited by - Axiverse on October 6, 2005 7:23:44 PM]

Share this post


Link to post
Share on other sites
ZQJ    496
The syntax for function pointers is:

int (*func) ();

So your example should read:


class Dele // line 6
{
public:

Dele(int (*func)()) : _function(func)
{

}


int (*_function)();
};


I don't know about assigning function pointers by construction though - you might have to put that inside the constructor and put:

_function = func

Share this post


Link to post
Share on other sites
Axiverse    366
Umm, another problem (templates this time... don't want to start another post?)

say i want

template<class a>
class MyClass
{

}

and then

template<class a, class b>
class MyClass
{

}

then would i have to do the following?


template<class a>
class MyClass<a>
{

}


template<class a, class b>
class MyClass<a, b>
{

}

on all the classes, or all but the first?

Share this post


Link to post
Share on other sites
stylin    758
Quote:
Original post by ZQJ
I don't know about assigning function pointers by construction though - you might have to put that inside the constructor and put:

_function = func

No: a variable is a pointer is a function pointer. Just like any other pointer (or data member) you should if possible initialize it in the initialization list.

Axiverse: It may help to typedef your function pointers to avoid the sometimes convoluted syntax.

int some_function( float, double );

typedef int (* i_pfunction_fd)( float, double );
i_pfunction_fd psome_function = &some_function;

Share this post


Link to post
Share on other sites
Mushu    1396
When you declare a templated class:

template <class T>
class cFoo {
// 1 template parameter
};

template <class T, class V>
class cFoo2 {
// has 2 template parameters
};

I'm not sure what you're trying to do with the second two code snippits you provided, but it looks like specialization. Explicit specialization is essentially you telling the compiler how to implement the template for a certain set of parameters, so that you can modify the behavior so that it works right.

An explicit template specialization is defined:

template <>
class cFoo<int> {
// will be instantiated for cFoo<int> instead of other version
};


Please add more detail as per what you would like to achieve with the templates (trying to write a callback class?)

Share this post


Link to post
Share on other sites
Axiverse    366
I'm trying to write a delegate class, and currentally with just

template<class a>
class MyClass
{

}

and then

template<class a, class b>
class MyClass
{

}

it will give me an error saying

error C2977: 'Axiverse::Core::Delegate' : too many template arguments

(the actual code has soooooooooo many defines, cause i don't want to hardcode the different template specializations)

Share this post


Link to post
Share on other sites
jfclavette    1058
Quote:
Original post by Axiverse
Oh why must function pointers discard every other rule in C++!!!

And thanks!


Just to clarify, they do NOT. The * is attached to the variable, not the type.

Take the following example:

int* foo, bar;

Here, foo is an int pointer, bar is a plain integer. That's why I use int *foo, usually.

The same applies for function pointers. The * goes just before the name. So it is consistent.

Share this post


Link to post
Share on other sites
stylin    758
Quote:
Original post by xMcBaiNx
Take the following example:

int* foo, bar;

Here, foo is an int pointer, bar is a plain integer. That's why I use int *foo, usually.

The same applies for function pointers. The * goes just before the name. So it is consistent.

Because it's read from right to left:
const int const * p; // p is a pointer that is const to an int that is const

The parenthesis has higher precedence than a type, that's why you need another set of parenthesis around a function pointer declaration:
int & (const * pf)(); // pf is a pointer that is const to function returning a reference to an int

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this