CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
      #1    
    Old June 11th, 2002, 08:02 AM
    PetrP PetrP is offline
    Member
     
    Join Date: Nov 2001
    Posts: 101
    PetrP is an unknown quantity at this point (<10)
    function as a parameter for function

    Hi all,

    I am using Visual C++ for a long time, but I have never use it. Can somebody explain me, how to create function, which calls function, which was given as a parameter?

    Something like this:

    Code:
    void MainFuncion(SecondFunction)
    {
         LocalFunction1();
         LocalFunction2();
         ...
         SecondFunction();
    }
    Can somebody tell me how to do it?

    Thx,

    PetrP.

    PS: I think that sample code will be the best way, how to learn it.
    Reply With Quote
      #2    
    Old June 11th, 2002, 08:16 AM
    Quintesence Quintesence is offline
    Member
     
    Join Date: Oct 2001
    Posts: 48
    Quintesence is an unknown quantity at this point (<10)
    Ok here ya go

    #include <stdio.h>

    // is the function passed around
    void Function(int Parameter)
    {
    printf("%u",Parameter);
    }

    // Receives the function pointer and calls it
    void CallThisFunction(void (*FunctionPtr)(int Parameter1))
    {
    FunctionPtr(0);
    }

    void main()
    {
    // Call the function passing the function pointer
    CallThisFunction(Function);
    }
    Reply With Quote
      #3    
    Old June 11th, 2002, 08:32 AM
    amag amag is offline
    Member
     
    Join Date: Jun 2002
    Location: Sweden
    Posts: 81
    amag is an unknown quantity at this point (<10)
    or you could do something like:

    template <typename fun_type>
    void MainFunction(fun_type SecondFunction)
    {
    SecondFunction();
    }

    where fun_type can be both function pointers or classes with operator () overloaded.
    Reply With Quote
      #4    
    Old June 11th, 2002, 08:51 AM
    Quintesence Quintesence is offline
    Member
     
    Join Date: Oct 2001
    Posts: 48
    Quintesence is an unknown quantity at this point (<10)
    Possibly, never tried it before. I'm not sure if it will work as a template. If it does it could be tricky when it comes to handling the function parameters unless you use the ... for parameter passing. I'd be interested to see if it does work.
    Reply With Quote
      #5    
    Old June 11th, 2002, 09:02 AM
    PetrP PetrP is offline
    Member
     
    Join Date: Nov 2001
    Posts: 101
    PetrP is an unknown quantity at this point (<10)
    Thanks to both,

    all possibilities were working fine.

    First one from Quintesence was working also with parameter passing, second one form amag was also working fine until I want pass the parameters.

    All these was working in console application. I tried it also in MFC classes and there were some problems. I have to little bit play with it.

    But I want to add it to the DLL so no MFC will be needed .

    Onece more,

    thanks to both.

    Petr P
    Reply With Quote
      #6    
    Old June 11th, 2002, 12:09 PM
    amag amag is offline
    Member
     
    Join Date: Jun 2002
    Location: Sweden
    Posts: 81
    amag is an unknown quantity at this point (<10)
    To be able to pass parameters just do:

    template <typename fun_type>
    void MainFunction(fun_type SecondFunction)
    {
    SecondFunction(10, "Hello");
    }

    or whatever. The compiler checks that the fun_type you pass to the function matches the arguments.
    In this case

    void my_fun(int k, const char *text)
    {
    cout << k << text << endl;
    }

    and then

    MainFunction(my_fun);

    would work just fine!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 08:51 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009