By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,463 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,463 IT Pros & Developers. It's quick & easy.

Question on arrays of pointers to functions in C++

P: n/a

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.
Apr 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
C. J. Clegg wrote:
[...]
// 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 10 '06 #2

P: n/a
C. J. Clegg <re****************@nospam.no> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Apr 10 '06 #3

P: n/a
On Mon, 10 Apr 2006 11:55:38 -0400, "Victor Bazarov"
<v.********@comAcast.net> wrote:
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...
Apr 10 '06 #4

P: n/a
C. J. Clegg <re****************@nospam.no> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Apr 10 '06 #5

P: n/a
C. J. Clegg wrote:
On Mon, 10 Apr 2006 11:55:38 -0400, "Victor Bazarov"
<v.********@comAcast.net> wrote:
[..]
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?


Copy and paste. I am betting your text editor and news reader allow you
to do so.
Apr 10 '06 #6

P: n/a
On Mon, 10 Apr 2006 18:48:20 +0200, "Jakob Bieling"
<ar****************@rot13.com> wrote:
C. J. Clegg <re****************@nospam.no> wrote:
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.


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.
Apr 11 '06 #7

P: n/a
On Mon, 10 Apr 2006 13:50:57 -0400, "Victor Bazarov"
<v.********@comAcast.net> wrote:
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. :-)
Apr 11 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.