For example:
struct Foo
{
int bar;
int (*baz)(int);
};
int testFunc(int x)
{
return x;
}
Foo list[] = {
{ 0, &testFunc },
{ 1, 0 } // no func for this.
};
In this example, I’d rather put the function directly into the list[] initializer rather than using a pointer to a function declared elsewhere; it keeps the related code/data in the same place.
Is there a way of doing this? I tried every syntax I could think of and couldn’t get it to work.
If you mean something like:
then, no, it’s not possible. You’re talking about anonymous functions, something C++ doesn’t yet support (as of August 2011).
C++0x is adding support for lambda functions, which is pretty much the same thing and your syntax would probably be something like:
However, if your intention is simply to keep the code and data in close proximity, then just keep them in close proximity (the same C source file, with the code immediately preceding the data).