Mailinglist Archive: opensuse-programming (118 mails)

< Previous Next >
Re: [suse-programming-e] C++ pointer to function confusion
  • From: John Lamb <J.D.Lamb@xxxxxxxxxxxxxx>
  • Date: Mon, 09 Jun 2003 17:10:12 +0100
  • Message-id: <3EE4B164.40108@xxxxxxxxxxxxxx>
Verdi March wrote:
Thanks to all.

So, instead of letting customization via inheritance, I simply add a
method to class Process that set the proper function, i.e
Process::setCustomFunction(int (*f)(int, char*[]));


I think you can use inheritance if you did something like the following. (The private base class constructor has a similar effect to using a pure virtual fucntion.)

class Process {
private:
typedef int (*callback)( int, char** );
protected:
Process( const callback run ) : run( run ){}
public:
const callback run;
};

class Test : public Process {
public:
Test() : Process( &Test::run ){}
private:
static int run( int x, char** c ){ ... }
};

This should let you use inheritance if you want. For example you could create an instance with:

Process* process = new Test();

and then call

cfunction( process->run );

and that should work. You could even make a subclass singleton if that's appropriate.

OTOH, this could be a rather longwinded way of creating a number of similar functions, especially if you don't actually need polymorphism.

--
JDL

Non enim propter gloriam, diuicias aut honores pugnamus set propter
libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.


< Previous Next >