Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

still no seccuss??? (function pointers)

Status
Not open for further replies.

EngineersForge

Technical User
Aug 26, 2003
15
CA
Ok thank you for the samples; however, I am still at a loss.

Here is another attempt:

calss a
{
public:
void (*fpReqSer) (void);
void RequestService(void);
};

void a::RequestService(void)
{
//raise event
fpReqSer();
}

NOW

class b: public a
{
whatever...
};

NOW

void service1(void){print some stuff out};

void main(void)
{
b Foo;
Foo.fpReqSer = &Service1;

pod.RequestService();//works just fine

b Foo1(Foo);
Foo1.RequestService();//access violation ???
}

I think the problem must be int he copy constructor impl.

HERE IT IS

a::a(const a &rhs)
{
//one of
*fpReqSer = *rhs.fpReqSer; // (1
fpReqSer = **rhs.fpReqSer; // (2
fpReqSer = (*rhs.fpReqSer)(); // (3
fpReqSer = (rhs::*fpReqSer)(); // (4
}

1) just sinc. the pointers??? It don't work! access violation at runtime.
2) because rhs is a reference??? don't work! access violation at runtime.
3) it complies, but don't work??? access violation at runtime.
3) for some reason complier doesn't recognize rhs as a class or a namespace???
There are other combinations that compile but will not run. Interesting thing is that they all send the IP to the same no-where address (0xcccccccc). I just can't see why one can't copy a pointer to a function from another class type of the same kind.

Could this be a VS C++ thing potentially used to call libraries and disassemble them to hack the code????

tanx
 
I posted an answer to your other thread.

The idea is that function pointers are the same as any other pointer. Please look at the following example.

int a;
int *a_ptr;
int b;
int *b_ptr;

a = 100;
a_ptr = &a; // now a = 100 and *a_ptr = 100
b = 200;
b_ptr = &b; // now b = 200 and *b_ptr = 200
// notice this difference, *a_ptr = 100,
// but a_ptr = 0x00213ab34c.
// if I want to copy the pointer a_ptr
// to the pointer b_ptr, I use the following
b_ptr = a_ptr; // now a_ptr and b_ptr both = 0x00213ab34c

Hope that helps.

-adeh
 
Well this is interesting.

when I add:
Foo.fpReqSer = NULL;

in:

Foo.fpReqSer = &Service1;

Foo.RequestService();//works just fine

Foo.fpReqSer = NULL; //new code

b Foo1(Foo);
Foo1.RequestService();//works just fine

Can't hold multiple references to a global function. Is it a threading question?
 
EF,

No, there is no problem about having multiple references to aglobal function. Your changes to the code do not make much sense, unless you also changed to copy constructor to the following
fpReqSer = rhs.fpReqSer;
which is what I asked you to do in the first place.

 
Sorry, yes I did change the code in the copy constructor. I had that originally, I was just trying different ways. This is what happens. I know what is right, but I start to doubt myself when I can't get it to work.

Whatever the case it does work now. The pointer has to be released first. That is why I think it might have something to do with the threading? Or maybe the type of function call the Service1() is, or the type of function call the pointer complies to? I think the pointer is a void (_cdecll*)(void)?
 
Right, it is void (__cdecl *)(void), but there should be no need to release the pointer or anything like that. Just trunst yourself, you'll be fine.

I would suggest that that you look into the C++ construct called functors, or function objects. They allow you to manage function pointers in a more understandable way.

-adevadeh
 
I think your class is the problem. That function pointer declared within the class will also have some class name mangling. It is really a method pointer, not a function pointer. Typedef the function pointer outside of the class, declare a new variable inside the class and try it.

Matt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top