c++中的构造函数可以返回类型吗 (c++指针对象调用构造函数)

以下是使用STL算法for_each()使用函数指针的实例:

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
// callback function
void PrintInt(int elem){
    cout << elem << ' ';
}
int main()
{
    vector<int> coll;
    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i) {
        coll.push_back(i);
    }
    // print all elements
    for_each (coll.begin(), coll.end(), // range
        PrintInt); // operation
    cout << endl;
    getchar();
}

PrintInt的参数是如何传递的呢?

以下是使用STL算法for_each()使用函数对象的实例:

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
// simple function object that prints the passed argument
class PrintInt {
public:
    void operator() (int elem) const {
        cout << elem << ' ';
    }
};
int main()
{
    vector<int> coll;
    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i) {
        coll.push_back(i);
    }
    // print all elements
    for_each (coll.begin(), coll.end(), // range
        PrintInt()); // operation
    cout << endl;
    getchar();
}

PrintInt的参数是如何传递的呢?同时,为什么只需要一个默认构造函数调用即可?

看一下for_each()的实现便清晰了:

namespace std {
    template <typename Iterator, typename Operation>
        Operation for_each (Iterator act, Iterator end, Operation op)
    {
        while (act != end) { // as long as not reached the end
            op(*act); // - call op() for actual element
            ++act; // - move iterator to the next element
        }
        return op;
    }
}

callback函数或function object在使用时,由caller进行调用,caller的参数通常用做callback函数或function object的参数。调用的默认构造函数生成了一个临时对象做实参,赋值给Operation op。

也可以由function object来提供状态信息:

#include <list>
#include <algorithm>
#include <iostream>

using namespace std;
// function object that adds the value with which it is initialized
class AddValue {
private:
    int theValue; // the value to add
public:
    // constructor initializes the value to add
    AddValue(int v) : theValue(v) {
    }
    // the ‘‘function call’’ for the element adds the value
    void operator() (int& elem) const {
        elem += theValue;
    }
};
template <typename T>
inline void PRINT_ELEMENTS (const T& coll,
                            const std::string& optstr="")
{
    std::cout << optstr;
    typename T::const_iterator pos; // not static
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    //for (const auto& elem : coll) { // c++11
        //std::cout << elem << ’ ’;
    }
    std::cout << std::endl;
}
int main()
{
    list<int> coll;
    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i) {
        coll.push_back(i);
    }
    PRINT_ELEMENTS(coll,"initialized: ");
    // add value 10 to each element
    for_each (coll.begin(), coll.end(), // range
        AddValue(10)); // operation
    PRINT_ELEMENTS(coll,"after adding 10: ");
    // add value of first element to each element
    for_each (coll.begin(), coll.end(), // range
        AddValue(*coll.begin())); // operation
    PRINT_ELEMENTS(coll,"after adding first element: ");
    getchar();
    return 0;
}
/*
initialized: 1 2 3 4 5 6 7 8 9 
after adding 10: 11 12 13 14 15 16 17 18 19 
after adding first element: 22 23 24 25 26 27 28 29 30 
*/

-End-