help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ class pointers to member function Help


From: Paul Pluzhnikov
Subject: Re: C++ class pointers to member function Help
Date: Sun, 12 Feb 2006 09:43:27 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

"swagat" <swagat.kumar@gmail.com> writes:

> I want to do something like this:

Your question isn't very clear. Perhaps you shoul read this:
www.catb.org/~esr/faqs/smart-questions.html

> class abc{
...
> };
>
> void update(double x)

void abc::update(double x)

> {
>           switch(i){
>              case 1:

That shouldn't compile:

>                        f=func1;

// error: cannot convert `double (abc::*)(double)' to `double (*)(double)' in 
assignment
...
>                        break;
>          }

presumably here you want to call the function through the pointer
just assigned, and don't know how to do so?

>  }
>
> I want to use a pointer to access the other member
> functions within the class as demonstrated above. Is it possible to do
> this?

I think, you want this:

class abc {
  int i;
  double a;
public:
  double func1(double x);
  double func2(double x);
  void update(double x);
};

void abc::update(double x)
{
  double (abc::*f)(double);
  switch (i) {
    case 1: f = &abc::func1; break;
    case 2: f = &abc::func2; break;
  }
  (this->*f)(x);
}

If that's *not* what you want, try to ask your question in a
clearer way.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]