[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

functionpointers to another class

Axel Gallus

10/7/2008 8:22:00 AM

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

5 Answers

anon

10/7/2008 8:28:00 AM

0

A.Gallus wrote:
> 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) ();
>

How is OtherClass defined?

> };
>
> Is this possible somehow?

Your example is missing main()

Anyway, you might want to read this:
http://www.parashift.com/c++-faq-lite/pointers-to-me...

Axel Gallus

10/7/2008 8:42:00 AM

0

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





"anon" <anon@no.invalid> schrieb im Newsbeitrag
news:gcf6hq$2b0$1@news01.versatel.de...
> A.Gallus wrote:
>> 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) ();
>>
>
> How is OtherClass defined?
>
>> };
>>
>> Is this possible somehow?
>
> Your example is missing main()
>
> Anyway, you might want to read this:
> http://www.parashift.com/c++-faq-lite/pointers-to-me...

anon

10/7/2008 9:08:00 AM

0

>
>
> "anon" <anon@no.invalid> schrieb im Newsbeitrag
> news:gcf6hq$2b0$1@news01.versatel.de...
>> A.Gallus wrote:
>>> 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) ();
>>>
>>
>> How is OtherClass defined?
>>
>>> };
>>>
>>> Is this possible somehow?
>>
>> Your example is missing main()
>>
>> Anyway, you might want to read this:
>> http://www.parashift.com/c++-faq-lite/pointers-to-me...
>

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

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)();
}

James Kanze

10/7/2008 11:47:00 AM

0

On Oct 7, 10:21 am, "A.Gallus" <u...@rz.uni-karlsruhe.de> wrote:
> 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()().

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Axel Gallus

10/7/2008 12:06:00 PM

0


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

"anon" <anon@no.invalid> schrieb im Newsbeitrag
news:gcf8tf$3ga$1@news01.versatel.de...
>>
>>
>> "anon" <anon@no.invalid> schrieb im Newsbeitrag
>> news:gcf6hq$2b0$1@news01.versatel.de...
>>> A.Gallus wrote:
>>>> 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) ();
>>>>
>>>
>>> How is OtherClass defined?
>>>
>>>> };
>>>>
>>>> Is this possible somehow?
>>>
>>> Your example is missing main()
>>>
>>> Anyway, you might want to read this:
>>> http://www.parashift.com/c++-faq-lite/pointers-to-me...
>>
>
> 1. Do not top post. Read:
> http://www.parashift.com/c++-faq-lite/how-to-post.ht...
>
> 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)();
> }