Results 1 to 7 of 7

Thread: Function declare for use of function as param

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271

    Function declare for use of function as param

    Ok if I declare my functions in a class as.

    SetEvent(long tEvent,void *funcname);

    MSVC lets me do
    void SomeFunc();
    void SomeotherFunc(int, int);
    ....
    SomeClass someobject;
    ....
    someobject.SetEvent(1,SomeFunc);
    someobject.SetEvent(1,SomeotherFunc);


    However Ming32 errors on both.
    I have to cast it which I dont want to do.

    What is the correct way to declare a function that takes a function as a parameter. Or is it possible?

    packetvb

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Not sure, but I think you have to make a pointer to that function, have you done that?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    Thats what I dont want to have to do.
    I dont want to cast it and I dont want to have to create a pointer for the function.
    I guess I want to declare in the function of the class to expect a pointer to a function.
    Hows that done?

  4. #4
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Not quite sure what you want, but i use this as a parameter for some of my functions.

    void (*<Variable Name>)(<Parameter List For Function>)

    You'd pass in something like &MyFunction
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    try something like this:


    Code:
    // pfMyFunction is a pointer to a void function that takes 2 int arguments
    typedef void(*pfMyFunction)(int a, int b);
    
    // this is an example of a pfMyFunction
    void MyFunction(int a, int b)
    {
    }
    
    void FuncAsArg(pfMyFunction func)
    {
    	func(1, 3);
    }
    
    
    int main()
    {
    	FuncAsArg(MyFunction);
    	return;
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    Thanks for the replies.

    SLH,
    Nope doesnt work Ming complains about the function def.

    sunburnt,
    Thats exacetly what I wanted to stay away from.


    Oh well, I guess ill just cast it using (void*) before the function names. That way itll work on any func decl.

    Kinda odd that MSVC excepts the func name only and Ming requires the cast. Just means Ill have to do a fix up for ming compilers after the conversion from basic to c++.

    Thanks guys/girls.

    packetvb

  7. #7
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    The code I showed does not cast to a void* -- it works on any function fitting the declaration void somefunc(int, int).
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width