开发者

boost lambda with a vector of shared pointers

Below is a slightly modified code from one good example how to copy values fro one vector of strings to another vector of objects. (see: another copy algorithm )

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using boost::shared_ptr;
using boost::make_shared;
using namespace boost::lambda;

class Object
{
public:
    std::string Name;

    Object(const std::string& Name_ = "")
        : Name(Name_)
    {
    }
};

int main()
{
    //std::vector<Object> objects(3, Object());
    std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

    std::vector<std::string> names;
    names.push_back("Alpha");
    names.push_back("Beta");
    names.push_back("Gamma");
    std::vector<std::string>::const_iterator names_it;
    names_it = static_cast<const std::vector<std::string>&>(names).begin();

    //std::for_each(objects.begin(), objects.end()开发者_开发知识库, bind(&Object::Name, _1) = *var(names_it)++);
    std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

    //std::vector<Object>::iterator it, end = objects.end();
    std::vector<shared_ptr<Object> >::iterator it, end = objects.end();
    for (it = objects.begin(); it != end; ++it) {
        //std::cout << it->Name << std::endl;
        std::cout << (*it)->Name << std::endl;
    }

    return EXIT_SUCCESS;
}

In this case I'm using dynamically allocated Objects, and because boost::lambda::bind can't handle such changes as boost::bind do, I need to dereference placeholder in order to compile:

std::for_each(objects.begin(), objects.end(), bind(&Object::Name, *_1) = *var(names_it)++);

But then at the output I've got:

Gamma
Gamma
Gamma

What is your explanation?


std::vector<shared_ptr<Object> > objects(3, make_shared<Object>());

This is equivalent to doing:

shared_ptr<Object> object = make_shared<Object>();
std::vector<shared_ptr<Object> > objects(3, object);

The vector constructor will then make 3 copies of that object pointer, which will all refer to same one and unique Object (that is to say, *object). Fill the vector with separate pointers each initialized with their own Object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜