Results 1 to 4 of 4

Thread: Passing function parameter and static

  1. #1
    Join Date
    Jun 2009
    Posts
    50

    Passing function parameter and static

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    struct Functor{
    bool operator()(const std::string& str){cout<<str<<endl;return true;}
    };
    struct Functor2{
    static bool compute(const std::string& str){cout<<str<<endl;return true;}
    };
    struct Foo
    {
    template<class F>
    void callMe()
    {
     F()("operator ()");
    }
    template<class F>
    void callMe2()
    {
     F::compute("static");
    }
    };
    int main(int n,char ** arfv)
    {
    Foo obj;
    obj.callMe<Functor>();
    obj.callMe2<Functor2>();
    system("pause");
    }
    If you want to pass a function parameter which method is better to use: functor or functor2?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Passing function parameter and static

    What is static over here? Static method means a state where the compiler does not allow any reentry. That means, you cannot change anything in that method or variable. In short, none of the two is recommended. The idiom favored by the standard:

    Code:
    template<class Fun> void callMe( Func f)
    {
    f("operator ()" );
    }

  3. #3
    Join Date
    Jun 2009
    Posts
    50

    Re: Passing function parameter and static

    I recently read a language such as ADA which is assumed to be better than C++ programming for parallel/concurrent. And with the next version of C++ or the development of third-lib, we can not hope that one day the C++ is doing better in this area? Any comments?

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Passing function parameter and static

    Quote Originally Posted by Banjiji View Post
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    struct Functor{
    bool operator()(const std::string& str){cout<<str<<endl;return true;}
    };
    struct Functor2{
    static bool compute(const std::string& str){cout<<str<<endl;return true;}
    };
    struct Foo
    {
    template<class F>
    void callMe()
    {
     F()("operator ()");
    }
    template<class F>
    void callMe2()
    {
     F::compute("static");
    }
    };
    int main(int n,char ** arfv)
    {
    Foo obj;
    obj.callMe<Functor>();
    obj.callMe2<Functor2>();
    system("pause");
    }
    If you want to pass a function parameter which method is better to use: functor or functor2?
    do not use static data (unless they are constants), using only data from the caller, do not call next function

    Regarding ADA:

    Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages. Ada is strongly typed and compilers are validated for reliability in mission-critical applications, such as avionics software.

Similar Threads

  1. Passing object as a parameter in a method
    By Karumbu in forum Software Development
    Replies: 4
    Last Post: 06-01-2011, 01:07 AM
  2. Passing an ArrayList From the Form as Parameter
    By Sheenas in forum Software Development
    Replies: 8
    Last Post: 20-06-2010, 10:45 AM
  3. Passing "file" parameter in a function
    By kamina23 in forum Software Development
    Replies: 3
    Last Post: 29-10-2009, 11:07 PM
  4. Passing parameter of Oracle procedure in PHP
    By Halina in forum Software Development
    Replies: 3
    Last Post: 22-04-2009, 08:46 PM
  5. Parameter passing using NavigateURL
    By Sachit in forum Software Development
    Replies: 3
    Last Post: 26-03-2009, 09:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,029,083.61370 seconds with 17 queries