using and de-referencing a function pointer in a member function

L

Lars Uffmann

Hi everyone,

I'm trying to execute the same function call with 1 parameter different
in a switch statement of a member function. However, if a certain
condition is true (and I can check that before the switch statement), I
want to execute a difference function call with the same parameters.

So instead of using an if-clause to distinguish the 2 cases in each
case-statement, or doubling the switch, I considered this:

void configuration::add (string param, configParameter cp);
void configuration::set (string param, configParameter cp);

my member function:

void configuration::add (string param, string strType, string strValue);
{
void (configuration::*add_or_set)(string, configParameter);

if (/* my condition */) add_or_set = &configuration::add;
else add_or_set = &configuration::set;

/* then the switch statement */
switch (expression) {
case a: (this->*add_or_set) (someString, someConfigParameter);
break;
case b: (this->*add_or_set) (someOtherString, someConfigParameter);
break;
default:
break;
}
}

After I played around with the de-referencing for a bit, getting various
error messages, I finally got it right (seemingly) with this expression
(this->*add_or_set) (parameters ...)

So my question: Is this the way to go? Or should I use a different
syntax? Thank you in advance!

Lars
 
L

Lars Uffmann

Victor said:
Yes, it's the only syntax available to you directly. You can, of
course, declare a 'configuration&' and init it by *this, but you will
still have to use the parens:

Thank you!

void configuration::add (string param, configParameter cp);
> [..]
case a: (self.*add_or_set)(someString, someConfigParameter);
Now, let me be the first to note that you should probably pass those
strings by reference to const...

Hmmm - I'm not really using by reference to const parameters at all yet
- are you only talking about a way to save computing time by evading a
copy construction of a new string? Or is there something else involved?

Best Regards,

Lars
 
L

Lars Uffmann

Victor said:

Because there is a bazillion aspects to learn about a programming
language, and learning by doing you only learn stuff as you need it or
read about it. I simply didn't need it yet (since 1993ish, but then I
did a lot in other languages over the years) and wasn't worried about
the speed issue here, but as you can see:
- are you only talking about a way to save computing time by evading a
copy construction of a new string? Or is there something else involved?
Uh... Yeah... I am talking about the choice you had
[..]
which is a bit better (generally speaking). When you can avoid making a
copy, you should.

I was able to make an educated guess at your comments purpose, and will
be using this in the future, if applicable.


Best Regards,

Lars
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top