functionpointers to another class

A

A.Gallus

I want to define a functor which calls methods of other classes via a
function pointer:

class play

{

public:

void operator ()

{

(*this.*fp)();

}

private:

void (OtherClass::*fp) ();

};

Is this possible somehow?

Regards

R4DIUM
 
A

A.Gallus

class SomeClass

{

public:

void somefunc();

};



class play

{

public:

play( SomeClass * sc ){ this->scthis = sc; }

void operator ()

{

(*scthis.*fp)();

}

set_fp( void(SomeClass::*newfp)() )

{

this->fp = newfp;

}

private:

SomeClass * scthis;
void (SomeClass::*fp) ();

};


void main()

{

SomeClass * SC = new SomeClass();
play * p = new play(SC);
p->set_fp( &SomeClass::somefunc );
(*p)();

}

This will not work, just to sketch the idea...

regards

R4DIUM
 
A

anon

1. Do not top post. Read:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4

2. You should increase the warning level of your compiler and read
compiler errors carefully.
I copy&paste your example, but when I tried to compile your example (g++
4.1.3 without any additional options), I got this:
b2.cpp:21: error: invalid member function declaration
b2.cpp:29: error: ISO C++ forbids declaration of ‘set_fp’ with no type
b2.cpp:45: error: ‘::main’ must return ‘int’
b2.cpp: In function ‘int main()’:
b2.cpp:52: error: no match for call to ‘(play) ()’

Here is the fixed version of your example:

class SomeClass
{
public:
void somefunc()
{
}
};
class play
{
public:
play( SomeClass * sc ){ this->scthis = sc; }
void operator () ()
{
(*scthis.*fp)();
}
void set_fp( void(SomeClass::*newfp)() )
{
this->fp = newfp;
}
private:
SomeClass * scthis;
void (SomeClass::*fp) ();
};

int main()
{
SomeClass * SC = new SomeClass();
play * p = new play(SC);
p->set_fp( &SomeClass::somefunc );
(*p)();
}
 
J

James Kanze

I want to define a functor which calls methods of other classes via a
function pointer:
class play
{
public:
void operator ()
{
(*this.*fp)();
}
private:
void (OtherClass::*fp) ();
};
Is this possible somehow?

Of course. You're on the right track, the only thing is in the
operator()():

1. You need an object of type OtherClass (or a pointer to such
an object); you can't use a pointer to a member of
OtherClass with this (which is a pointer to play).

2. Your syntax isn't quite right for the call. If you have a
pointer, then it would be (otherObject->*fp)() if you have
a reference or an object, (otherObject.*fp)().

3. And also, it should be operator()().
 
A

A.Gallus

I didn't compile the code, because I only wanted to know if the basic idea
is right.
Sorry for inconvenience and thx.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top