473,386 Members | 1,842 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.

Callback to a non-static method?

Hi,

(I hope that this is not a FAQ)

I'm trying to make a timer call a method of an object.

void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}

void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

This does not work. The Error is
"argument of type 'void (MyClass::)(int)' does not match 'void (*)(int)'"

Why is that not working? Is there a workaround?

Thanks,
Levin
Jul 22 '05 #1
16 2603
* "Levin Alexander" <le***@grundeis.net> schriebt:

I'm trying to make a timer call a method of an object.

void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}

void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

This does not work. The Error is
"argument of type 'void (MyClass::)(int)' does not match 'void (*)(int)'"


Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.

So the technical error prevented you from making a much more serious
and much more difficult to pin down error.

Many others will probably explain how to do The Technical Thing (TM)
to get the code to compile (thus introducing the Grave Error (TM)).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
* "Levin Alexander" <le***@grundeis.net> schriebt:

I'm trying to make a timer call a method of an object.

void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}

void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

This does not work. The Error is
"argument of type 'void (MyClass::)(int)' does not match 'void (*)(int)'"


Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.

So the technical error prevented you from making a much more serious
and much more difficult to pin down error.

Many others will probably explain how to do The Technical Thing (TM)
to get the code to compile (thus introducing the Grave Error (TM)).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

"Levin Alexander" <le***@grundeis.net> wrote in message
news:op**************@news.rwth-aachen.de...
Hi,

(I hope that this is not a FAQ)


Err it is.

http://www.parashift.com/c++-faq-lit....html#faq-33.2

john
Jul 22 '05 #4

"Levin Alexander" <le***@grundeis.net> wrote in message
news:op**************@news.rwth-aachen.de...
Hi,

(I hope that this is not a FAQ)


Err it is.

http://www.parashift.com/c++-faq-lit....html#faq-33.2

john
Jul 22 '05 #5
Alf P. Steinbach <al***@start.no> wrote:
void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}
void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.


(I sense the need to buy a decent book.)

Is there a "pure" C++ way to solve that problem?

Thanks,
Levin
Jul 22 '05 #6
Alf P. Steinbach <al***@start.no> wrote:
void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}
void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.


(I sense the need to buy a decent book.)

Is there a "pure" C++ way to solve that problem?

Thanks,
Levin
Jul 22 '05 #7
* "Levin Alexander" <le***@grundeis.net> schriebt:
Alf P. Steinbach <al***@start.no> wrote:
void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}
void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.


(I sense the need to buy a decent book.)

Is there a "pure" C++ way to solve that problem?


Not "pure", but by using platform-specific functionality you can implement
timers that are, for example, exception safe.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8
* "Levin Alexander" <le***@grundeis.net> schriebt:
Alf P. Steinbach <al***@start.no> wrote:
void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}
void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.


(I sense the need to buy a decent book.)

Is there a "pure" C++ way to solve that problem?


Not "pure", but by using platform-specific functionality you can implement
timers that are, for example, exception safe.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #9

"Levin Alexander" <le***@grundeis.net> wrote in message
news:op**************@news.rwth-aachen.de...
Hi,

(I hope that this is not a FAQ)

I'm trying to make a timer call a method of an object.

void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}

void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

This does not work. The Error is
"argument of type 'void (MyClass::)(int)' does not match 'void (*)(int)'"

Why is that not working? Is there a workaround?

Thanks,
Levin


You could have one static member which takes a pointer to the type of class
it belongs, then you could pass it the 'this' pointer.

e.g.

void MyClass::Callback(MyClass *xiThis, ...)
{
xiThis->NonStaticCallback(...);
}

HTH
Allan
Jul 22 '05 #10

"Levin Alexander" <le***@grundeis.net> wrote in message
news:op**************@news.rwth-aachen.de...
Hi,

(I hope that this is not a FAQ)

I'm trying to make a timer call a method of an object.

void MyClass::myCallback(int par)
{cout << "Callback Called" << std::endl;}

void MyClass::addTimer()
{
signal(SIGALRM, myCallback);
// ...
}

This does not work. The Error is
"argument of type 'void (MyClass::)(int)' does not match 'void (*)(int)'"

Why is that not working? Is there a workaround?

Thanks,
Levin


You could have one static member which takes a pointer to the type of class
it belongs, then you could pass it the 'this' pointer.

e.g.

void MyClass::Callback(MyClass *xiThis, ...)
{
xiThis->NonStaticCallback(...);
}

HTH
Allan
Jul 22 '05 #11
* "Allan Bruce" <al*****@TAKEAWAYf2s.com> schriebt:

You could have one static member which takes a pointer to the type of class
it belongs, then you could pass it the 'this' pointer.

e.g.

void MyClass::Callback(MyClass *xiThis, ...)
{
xiThis->NonStaticCallback(...);
}


Although the above will work with all relevant compilers (AFAIK), it's
officially ungood. A C callback needs to be 'extern "C"'. Hence
it needs to be a namespace scope function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #12
* "Allan Bruce" <al*****@TAKEAWAYf2s.com> schriebt:

You could have one static member which takes a pointer to the type of class
it belongs, then you could pass it the 'this' pointer.

e.g.

void MyClass::Callback(MyClass *xiThis, ...)
{
xiThis->NonStaticCallback(...);
}


Although the above will work with all relevant compilers (AFAIK), it's
officially ungood. A C callback needs to be 'extern "C"'. Hence
it needs to be a namespace scope function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #13
>Many others will probably explain how to do The Technical Thing (TM)
to get the code to compile (thus introducing the Grave Error (TM)).


Could you explain further please?

What is The Technical Thing?

What is The Grave Error?
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #14
>Many others will probably explain how to do The Technical Thing (TM)
to get the code to compile (thus introducing the Grave Error (TM)).


Could you explain further please?

What is The Technical Thing?

What is The Grave Error?
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
Jul 22 '05 #15
* da*********@aol.com (DaKoadMunky) schriebt:
Many others will probably explain how to do The Technical Thing (TM)
to get the code to compile (thus introducing the Grave Error (TM)).
Could you explain further please?

What is The Technical Thing?


Explained by others in this thread, as predicted.

What is The Grave Error?
All explained by the part you snipped,

Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.


C signals are asynchronous, meaning a signal can occur at any point in
the machine code execution, for example at a point where the global
data structures, including structures related to e.g. exception handling
and maintained by compiler-inserted code, are not in well-defined states.

That means that there's really very little you can safely do in a signal
handler except set some global POD flags or terminate the process.

For example, i/o via the standard library is in general not a good idea.

And in particular, it's absolutely not a good idea to propagate an
exception out of a signal handler, and it may (but just may) be disastrous
to have any kind of exception handling inside it, which precludes use of
most of the standard library.

Also, signal handlers are traditionally associated with use of longjmp,
which isn't supported in C++ code; e.g. you're not guaranteed that stack
based objects will be properly destroyed (have their destructors called).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #16
* da*********@aol.com (DaKoadMunky) schriebt:
Many others will probably explain how to do The Technical Thing (TM)
to get the code to compile (thus introducing the Grave Error (TM)).
Could you explain further please?

What is The Technical Thing?


Explained by others in this thread, as predicted.

What is The Grave Error?
All explained by the part you snipped,

Be glad you got a compilation error, because mixing C signals and C++
object-oriented code is simply not supported, and leads to UB.


C signals are asynchronous, meaning a signal can occur at any point in
the machine code execution, for example at a point where the global
data structures, including structures related to e.g. exception handling
and maintained by compiler-inserted code, are not in well-defined states.

That means that there's really very little you can safely do in a signal
handler except set some global POD flags or terminate the process.

For example, i/o via the standard library is in general not a good idea.

And in particular, it's absolutely not a good idea to propagate an
exception out of a signal handler, and it may (but just may) be disastrous
to have any kind of exception handling inside it, which precludes use of
most of the standard library.

Also, signal handlers are traditionally associated with use of longjmp,
which isn't supported in C++ code; e.g. you're not guaranteed that stack
based objects will be properly destroyed (have their destructors called).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #17

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

Similar topics

7
by: Steven T. Hatton | last post by:
Can someone provide a fromal definition of "callback"? I see this term used often, and can usually understand the discussion where it's used, but I would be hard-pressed to provide a formal...
16
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is...
3
by: James Hu | last post by:
Hi, gurus, I would like to use ctypes to implement callback function for QImage Camera to capture image asynchronously, and I have the c++ code of callback, but I am totally in the dark, the...
7
by: andthen | last post by:
I'm using a windows library function which does not return anything useful (EnumWindows... returns a BOOL), I give it a pointer to a callback function and it sends the data I want to the callback...
5
by: DevarajA | last post by:
Is it possible to write a function that, once called, is executed together with the caller without stopping it? I've seen such functions in windows programming, using CALLBACK qualifier before the...
1
by: John | last post by:
Hi all, I'm trying to get a client callback to work and am able to get it to work when I comment out a section of other, non-related-client-callback code. Within my "If not page.ispostback"...
0
by: John | last post by:
Hi all, Perhaps I'm being a little impatient here but I needed to re-send with a metter of urgency... I'm trying to get a client callback to work and am able to get it to work when I comment...
2
by: David | last post by:
Hi all, I am new to .Net environment. I have created a flat non-COM DLL from Visual C++ 6.0. It stores up a function pointer from caller, create a worker thread via WIN32 API, and then call...
0
by: draskin | last post by:
Hello, I have the following situation: - Text.exe creates a new AppDomain - Test.exe loads mixed mode assembly in a non-default AppDomain - Test.exe calls connect to a TCP/IP socket server which...
18
by: kid joe | last post by:
Hello, I have seen the WndProc prototyped two ways, LONG WINAPI WndProc ( .... ); and LRESULT CALLBACK WndProc ( .... ); Are these functionally equivalent? What are WINAPI and CALLBACK...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.