473,385 Members | 1,521 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,385 software developers and data experts.

Pointer-to-member-function code okay?

I am compiling under Visual C++ 2005 Express (with the Platform SDK
installed).

I have a class that is responsible for creating and repeatedly running
a Timeslice() method using a thread:

class cProcessBlock : public cBase
{
public:
. . .
virtual void Timeslice(); // executed repeatedly by a 'while' loop
in a thread
};

I also declare a function pointer typedef to handle cProcessBlock callbacks:

typedef void (cProcessBlock::*PortCallbackFnPtr)(shared_ptr<cBa se>);

cProcessBlock has a descendant, cPort that registers callback methods
(in an STL map) and is responsible for calling these when objects are
received:

class cPort : public cProcessBlock
{
public:
. . .
void cPort::Timeslice()
// Continuously receive new objects and process using registered
callbacks
{
shared_ptr<cBase> pNewObject;
pNewObject = Receive();

// Call appropriate callback,
// depending on the received object's class name
if (pNewObject) {
PortCallbackFnPtr CallbackFn =
m_CallbackRegistry[pNewObject->ClassName()];
if (CallbackFn) // if callback handler is found
((*this).*(CallbackFn))(pNewObject);
}
}
map<string, PortCallbackFnPtr> m_CallbackRegistry;
}

I then define a descendant of cProcessBlock and create an instance:

class cBlockRx : public cProcessBlock {
public:
cBlockRx(. . .) {
m_pPort = shared_ptr<cPort>(new cPort);
m_pPort->AddCallback(static_cast<PortCallbackFnPtr>
(&cBlockRx::RxCallback), "cSomeObject");
m_streamOutput.open("Test.txt", ios::out);
};
void RxCallback(shared_ptr<cBase> pRx) {
m_streamOutput << "Got something" << endl << flush;
};

shared_ptr<cPort> m_pPort;
ofstream m_streamOutput;
} BlockRx;

When I start up the code that causes BlockRx to receive cSomeObjects,
the callback is entered correctly, although the output to the file
stream (m_streamOutput) bombs with an unhandled exception (access
violation) deep in the MS ostream code (the line begins "streamsize _Pad").

It bombs when I try a couple of other options in the callback too, which
leads me to suspect that I'm doing something nasty to the stack.

I guess I'm asking if the function pointer code above looks good, or if
there are reasons why I might be inviting "undefined behaviour".

I have also tried:
((*this).*static_cast<void(cProcessBlock::*)(share d_ptr<cBase>)>(CallbackFn))(pNewObject);
and ((*(static_cast<cProcessBlock*>(this))).*(Callback Fn))(pNewObject);
at the point where the callback is called. These were no better.

Thanks for any help!

Ryan
Mar 17 '06 #1
0 1405

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

Similar topics

6
by: Nico Kruithof | last post by:
Hello, I have the following problem. Assume I have a templated class (B) that is parameterized by a pointer to another class (A), how can I obtain information of the types of A within B? This...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
7
by: herrcho | last post by:
#include <stdio.h> int main() { printf("%s",*("abc" + 1)); return 0; } i expected this would show 'bc'
3
by: signuts | last post by:
I am wondering what it means when a pointer is aligned? Could someone perhaps enlighten me or point me in the right direction? Thank you in advance. -- Sig
27
by: Riaan Cillié | last post by:
Hi I'm trying to learn C, but I am struggling with using scanf and a struct. I want to define a structure and declare a variable of that type in int main. This has to be passed to a function and...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
20
by: Bill Potter | last post by:
I am a learning programmer in C and i want to know why some one would use pointers instead of going direct!
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
18
by: c.a.l | last post by:
a) If i do pointer = pointer_to_safe_thing - 1000; pointer == pointer_to_safe_thing; then I am *not* accessing invalid memory. Nor i am incorrect in mathematical sense. Nevertheless outcome...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.