Question on arrays of pointers to functions in C++

C

C. J. Clegg

Consider the following code...

class MyClass
{
public:
int Fct1( void );
int Fct2( void );
int Fct3( void );

void ExecFcts( void );
};

int MyClass::fct1( void )
{
// do some stuff
return 0
}

int MyClass::fct2( void )
{
// do some different stuff
return 0
}

int MyClass::fct3( void )
{
// do some still different stuff
return 0
}

void MyClass::ExecFcts( void )
{
struct
{
int anIntVar;
int (MyClass::*TheFct)( void );
} arrayOfFcts[ ] =
{
{ 0, &MyClass::Fct1 },
{ 1, &MyClass::Fct2 },
{ 2, &MyClass::Fct3 }
};

// This all compiles OK up to here.
// Then...
//
// try to call one of the functions in the struct array

arrayOfFcts[ 0 ].TheFct( );
// This makes a compile error
}

int main( int argc, char** argv )
{
MyClass theClass;

theClass.ExecFcts();

return 0;
}

I have read through the FAQ and every post I can find on dejanews on
this topic, in fact the FAQ educated me on the difference between a
pointer to a member function and a pointer to a non-member function
and the need to prepend "MyClass::" to the function names in the
struct array, and allowed me to get a clean compile up to the point
indicated in the code comment above.

But, I can't get the correct syntax for the function call.

I have tried every combination I can think of of:

arrayOfFcts[ 0 ].MyClass::TheFct( );
arrayOfFcts[ 0 ].*MyClass::TheFct( );
arrayOfFcts[ 0 ].MyClass::*TheFct( );

( ... with and without parens sprinkled liberally about...)

....and no luck.

I have done my research ... honest ... and I'm out of ideas.
 
V

Victor Bazarov

C. J. Clegg said:
[...]
// try to call one of the functions in the struct array

arrayOfFcts[ 0 ].TheFct( );

Should be

(this->*(arrayOfFcts[0].TheFct))();
// This makes a compile error
}
[...]

And next time please don't type your code directly into the posting,
you made several typos.

V
 
J

Jakob Bieling

C. J. Clegg said:
Consider the following code...

class MyClass
{
public:
int Fct1( void );
int Fct2( void );
int Fct3( void );

void ExecFcts( void );
};

int MyClass::fct1( void )
{
// do some stuff
return 0
}

int MyClass::fct2( void )
{
// do some different stuff
return 0
}

int MyClass::fct3( void )
{
// do some still different stuff
return 0
}

void MyClass::ExecFcts( void )
{
struct
{
int anIntVar;
int (MyClass::*TheFct)( void );
} arrayOfFcts[ ] =
{
{ 0, &MyClass::Fct1 },
{ 1, &MyClass::Fct2 },
{ 2, &MyClass::Fct3 }
};

// This all compiles OK up to here.
// Then...
//
// try to call one of the functions in the struct array

arrayOfFcts[ 0 ].TheFct( );
// This makes a compile error
}

int main( int argc, char** argv )
{
MyClass theClass;

theClass.ExecFcts();

return 0;
}

I have read through the FAQ and every post I can find on dejanews on
this topic, in fact the FAQ educated me on the difference between a
pointer to a member function and a pointer to a non-member function
and the need to prepend "MyClass::" to the function names in the
struct array, and allowed me to get a clean compile up to the point
indicated in the code comment above.

But, I can't get the correct syntax for the function call.

I have tried every combination I can think of of:

arrayOfFcts[ 0 ].MyClass::TheFct( );
arrayOfFcts[ 0 ].*MyClass::TheFct( );
arrayOfFcts[ 0 ].MyClass::*TheFct( );

Almost. I suppose you know that member functions need an object to
work with, considering you did the research :) In your code, you do not
provide any object, though. This is why this won't work. Try this
instead:

(this->*arrayOfFcts[0].TheFct) ();

For a 'regular' call (at the same code location!, ie. inside a
member function), as in 'Fct1 ();', you do not need to explicitly
provide an object, because 'this' is used implicitly. When you use
pointers, you must provide the object explicitly .. even inside a member
function.

hth
 
C

C. J. Clegg

Should be

(this->*(arrayOfFcts[0].TheFct))();

Good afternoon, Victor and Jakob.

Thanks to both of you for that... it's the one variation I didn't try
(naturally), and it worked fine.
And next time please don't type your code directly into the posting,

Hmmm ... Really? OK, I can refrain from doing that in the future, but
how do I provide code samples that illustrate the problem?

Thanks again...
 
J

Jakob Bieling

C. J. Clegg said:
how do I provide code samples that illustrate the problem?

You should compile the code until the only errors (if any) left are
the ones you are having problems with.

regards
 
V

Victor Bazarov

C. J. Clegg said:
On Mon, 10 Apr 2006 11:55:38 -0400, "Victor Bazarov"


Hmmm ... Really? OK, I can refrain from doing that in the future, but
how do I provide code samples that illustrate the problem?

Copy and paste. I am betting your text editor and news reader allow you
to do so.
 
C

C. J. Clegg

You should compile the code until the only errors (if any) left are
the ones you are having problems with.

Ah, OK, understood.

I actually did that, sort of ... the code I put in my posting (I guess
nobody calls these postings "articles" anymore) was a vastly
oversimplified version of some code that is in an application I'm
writing, and I figured you guys wouldn't want to wade through all the
baggage that comes with the real thing, so I simplified it to the
simplest possible form and used that. I did compile the actual code
but didn't compile my simplified sample first ... sorry.
 
C

C. J. Clegg

Copy and paste. I am betting your text editor and news reader allow you
to do so.

Ah, OK, I misunderstood what you said, thought you meant not to put
the code in the posting at all.

Got it. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top