473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

functionpointers to another class

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

Oct 7 '08 #1
5 1462
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-lit...o-members.html
Oct 7 '08 #2
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" <an**@no.invalidschrieb im Newsbeitrag
news:gc**********@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-lit...o-members.html
Oct 7 '08 #3
>
>
"anon" <an**@no.invalidschrieb im Newsbeitrag
news:gc**********@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-lit...o-members.html
1. Do not top post. Read:
http://www.parashift.com/c++-faq-lit...t.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)();
}
Oct 7 '08 #4
On Oct 7, 10:21 am, "A.Gallus" <u...@rz.uni-karlsruhe.dewrote:
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:ja*********@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
Oct 7 '08 #5

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

"anon" <an**@no.invalidschrieb im Newsbeitrag
news:gc**********@news01.versatel.de...
>>

"anon" <an**@no.invalidschrieb im Newsbeitrag
news:gc**********@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-lit...o-members.html

1. Do not top post. Read:
http://www.parashift.com/c++-faq-lit...t.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)();
}
Oct 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stewart Midwinter | last post by:
I've got an app that creates an object in its main class (it also creates a GUI). My problem is that I need to pass this object, a list, to a dialog that is implemented as a second class. I want...
5
by: Edward Diener | last post by:
This has occurred in MC++, but since there is very little response on that NG, I am also reporting it here in the hope that someone can find me a workaround, and report it to MS. If a __value...
11
by: Bob Rock | last post by:
Hello, I'd like to be able to allow instanciation of a class (class Class_A) only from another class method (class Class_B). And I'd like to write the necessary code to enforce this behavior...
6
by: SearedIce | last post by:
Consider the following simplified hypothetical code: #include <iostream.h> class rabbit { public: rabbit() {x = 3; y = 3; /*code here to set field to 1*/} void runtocage(); int x;
4
by: Arne Claus | last post by:
Hi I got a headache on this problem, maybe someone can help me here I want to create a class which can be handled like this manager<int, list> // possible variant manager<int, vector> //...
5
by: M O J O | last post by:
Hi, I want to expose a enum from another class, is that possible and how? Here's an example Public Class ClassMaster Public Enum Colors
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
3
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.