?calling function through pointer in a class 
Author Message
 ?calling function through pointer in a class

C doesn't have classes. Maybe you were thinking of comp.lang.c++?

<< I am trying to create a global pointer to a member function such that any
class can call that function while remaining ignorant of the class in
general. >>

This is not possible except under very special, limited circumstances.
--

Paul Lutus
www.arachnoid.com


<snip>



Tue, 02 Oct 2001 03:00:00 GMT  
 ?calling function through pointer in a class

C++ experts ready and willing to answer your question much more usefully
than we could do in this group, which is concerned with C rather than C++.


Quote:
>I am trying to create a global pointer to a member function such that any
>class

<snip>


Tue, 02 Oct 2001 03:00:00 GMT  
 ?calling function through pointer in a class

Quote:

>I am trying to create a global pointer to a member function such that any
>class can call that function while remaining ignorant of the class in
>general. This would be somewhat analagous to printf.

Dummkopf, don't you realize you are aking a C++ question in C newsgroup.

C doesn't have classes.

Quote:
>I have created a pointer to the function like this:

>void (hardware::* PrintAnalog) (double,int);

>And initialized it like this:

>hardware::hardware() {      PrintAnalog = AnalogOut; // class hardware has a
>member function AnalogOut() }

>BUT, when I try to call PrintAnalog from somewhere like this:

>   PrintAnalog (val,chan);

No kidding, in C++, pointer-to-member values must be dereferenced with respect
to a class object, like this.

        hardware object, *pobject;
        //...
        (object.*PrintAnalog)(val, chan);
        //...
        (pobject->*PrintAnalog)(val, chan);

Where do you think that that the ``this'' pointer is going to come from,
eh?

Look up the .* and ->* operators in your C++ reference manual. Keep in mind
that they have a low precedence, unlike . and ->, so the parentheses
are necessary.

What you are trying to accomplish can probably be done by a static function.  A
pointer to a static member function is ``ordinary''; it is not a
pointer-to-member, and calling it does not require an object, nor the special
.* or ->*  operators.



Tue, 02 Oct 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Inherited class function calls from a pointer assigned to the base class

2. How to use (pointer to function), and function and pointer to (pointer to function)

3. destructor is called before copy-constructor on temporary class (allocated on stack during function call)

4. Declaration of functions called via function pointers

5. Validity of simplified notation for calling functions from function pointers

6. newbie One class calling functions in another class

7. Class function pointer does not evaluate to a function

8. function pointers to functions in classes.

9. Function pointer to function inside class

10. how to let a global function pointer point to a function in a Class

11. Pointer Functions and Pointers to Pointer Functions

12. how to convert base class pointer to sub class pointer

 

 
Powered by phpBB® Forum Software