Was ist ein callable object in C++?

Ich bin derzeit Studium der boost-threads. Und ich stieß auf, dass die thread-Klasse hat einen Konstruktor akzeptiert callable objects. Was sind aufrufbare Objekte?

class CallableClass
{
private:
    //Number of iterations
    int m_iterations;

public:

    //Default constructor
    CallableClass()
    {
        m_iterations=10;
    }

    //Constructor with number of iterations
    CallableClass(int iterations)
    {
        m_iterations=iterations;
    }

    //Copy constructor
    CallableClass(const CallableClass& source)
    {
        m_iterations=source.m_iterations;
    }

    //Destructor
    ~CallableClass()
    {
    }

    //Assignment operator
    CallableClass& operator = (const CallableClass& source)
    {
        m_iterations=source.m_iterations;
        return *this;
    }

    //Static function called by thread
    static void StaticFunction()
    {
        for (int i=0; i < 10; i++)  //Hard-coded upper limit
        {
            cout<<i<<"Do something in parallel (Static function)."<<endl;
            boost::this_thread::yield(); //'yield' discussed in section 18.6
        }
    }

    //Operator() called by the thread
    void operator () ()
    {
        for (int i=0; i<m_iterations; i++)
        {
            cout<<i<<" - Do something in parallel (operator() )."<<endl;
            boost::this_thread::yield(); //'yield' discussed in section 18.6
        }
    }

};

Wie funktioniert dies ein callable-Objekt? Ist es, weil der Betreiber überlastet oder der Konstruktor oder etwas anderes?

  • Es ist, weil operator(), ja. Siehe hier. Neben "callable objects" in diesem Kontext ist auch, Funktionen, function-Pointer und lambda-Funktionen.
  • Es ist die überladene operator(). Es können Sie rufen Sie eine Instanz der Klasse wie eine Funktion. Sie heißen funktoren, die Funktion von Objekten, callable objects, etc.
  • Sollten Sie die tag-Nummer (und name) diese Frage mit boost. Boost ist nicht zu standard-C++ - Bibliothek, also das ganz große boost-community hier in der Lage sein könnte, um diesen pick-up schneller, wenn Sie getaggte und benannte Sie richtig.
InformationsquelleAutor Xegara | 2013-10-09
Schreibe einen Kommentar