Calling a function

Discussion in 'C++' started by sara, Jul 1, 2007.

  1. sara

    sara Guest

    Hi All,

    I have multiple classes and almost all of them want to call a single
    function. One way to do this is to create a single class consisting
    of that function and then initialize the class whenever I want to call
    the function. In this way the used class has only one member function.
    Is there a more efficient way?

    Thanks.
    sara, Jul 1, 2007
    #1
    1. Advertising

  2. sara escreveu:
    > Hi All,
    >
    > I have multiple classes and almost all of them want to call a single
    > function. One way to do this is to create a single class consisting
    > of that function and then initialize the class whenever I want to call
    > the function. In this way the used class has only one member function.
    > Is there a more efficient way?
    >
    > Thanks.
    >


    Choose one and let the efficiency to the compiler ...

    #include <iostream>

    using namespace std;

    void f1(char const *x)
    { cout << "f1: " << x << endl;
    }

    class f2
    {public:
    void operator()(char const *x) const
    { cout << "f2: " << x << endl;
    }
    };

    class MyFuncs
    {public:
    static void f3(char const *x)
    { cout << "f3: " << x << endl;
    }
    };

    int main()
    { f1("My test 1"); // The simplest
    f2()("My test 2"); // The trickyest
    MyFuncs::f3("My test 3"); // The OO'est
    return 0;
    }

    HTH
    Paulo da Silva
    Paulo da Silva, Jul 1, 2007
    #2
    1. Advertising

  3. On 2007-07-01 04:21, sara wrote:
    > Hi All,
    >
    > I have multiple classes and almost all of them want to call a single
    > function. One way to do this is to create a single class consisting
    > of that function and then initialize the class whenever I want to call
    > the function. In this way the used class has only one member function.
    > Is there a more efficient way?


    The idea of putting the method in a class and instantiating that class
    each time you want to call the method is the least efficient, and (in my
    opinion) the ugliest. If you go this road at least make the method
    static so you don't have to instantiate the class. But if you do it like
    this (a class with only one static method) I think you have a class that
    serves no purpose, so get rid of the class and make the method a free
    function (put it in a namespace if you don't want global functions).

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 1, 2007
    #3
  4. * sara:
    > Hi All,
    >
    > I have multiple classes and almost all of them want to call a single
    > function.


    A class doesn't do anything. An instance of a class does not do
    anything. A member function can do something.

    What did you mean to describe?


    > One way to do this is to create a single class consisting
    > of that function and then initialize the class whenever I want to call
    > the function.


    Classes are not initialized. They can be instantiated. You do not need
    to instantiate a class to call a static member function, and you don't
    need to let a function be a member of class.


    > In this way the used class has only one member function.
    > Is there a more efficient way?


    Depends what your question is about. Give an example.
    Alf P. Steinbach, Jul 1, 2007
    #4
  5. sara

    BobR Guest

    sara <> wrote in message...
    > Hi All,
    > I have multiple classes and almost all of them want to call a single
    > function. One way to do this is to create a single class consisting
    > of that function and then initialize the class whenever I want to call
    > the function. In this way the used class has only one member function.
    > Is there a more efficient way?
    > Thanks.
    >


    As others said, it depends on what you are trying to do.
    Just for an idea:

    #include <iostream>
    #include <vector>

    class Common{ public:
    virtual ~Common(){} // look up "rule of three"
    virtual void DoThing( std::eek:stream &out ){
    out<<"Do something here."<<std::endl;
    }
    };

    class First : public Common{ public:
    void Thing( std::eek:stream &out){
    out<<"class First: ";
    DoThing( out );
    }
    };
    class Second : public Common{ public:
    void Thing( std::eek:stream &out){
    out<<"class Second: ";
    DoThing( out );
    }
    };

    int main(){
    using std::cout; // for NG post
    First MyFirst;
    MyFirst.Thing( cout );
    Second MySecond;
    MySecond.Thing( cout );

    cout<<std::endl;
    std::vector<Common*> Rack;
    Rack.push_back( &MyFirst );
    Rack.push_back( &MySecond );
    for( std::size_t i(0); i < Rack.size(); ++i ){
    First *pF( dynamic_cast<First*>( Rack.at(i) ) );
    if( pF ){ pF->Thing( cout );}
    Second *pS( dynamic_cast<Second*>( Rack.at(i) ) );
    if( pS ){ pS->Thing( cout );}
    } // for(i)

    return 0;
    } // main()
    /* -output-
    class First: Do something here.
    class Second: Do something here.

    class First: Do something here.
    class Second: Do something here.
    */

    If you are worried about 'efficient', measure.

    --
    Bob R
    POVrookie
    BobR, Jul 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    572
  2. Andreas Lagemann
    Replies:
    8
    Views:
    451
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    423
  4. NoWonder
    Replies:
    2
    Views:
    287
    Matthias Kaeppler
    Apr 9, 2005
  5. Satish Chimakurthi

    Function calling another function

    Satish Chimakurthi, Mar 1, 2004, in forum: Python
    Replies:
    2
    Views:
    354
    Satish Chimakurthi
    Mar 1, 2004
Loading...

Share This Page