std::generate() functor argument with member functions

T

Tanguy Fautré

Hi,

given a class

class foo
{
private:
void A();
type B();
};

I want to call in A()
std::generate(V.begin(), V.end(), B);

where V is just a standard container of elements of the type "type", but it
does NOT necessarly contains instances of the class foo.

The idea is that B is a member function of foo but that the objects in the
container are not of the type foo.
I cannot make B static 'cause it uses members variables of the class foo.

I supposed I need something like std::generator(V.begin(), V.end(),
bind_func(* this, B)) but what exactly?

Best regards,

Tanguy
 
D

David White

Tanguy Fautré said:
Hi,

given a class

class foo
{
private:
void A();
type B();
};

I want to call in A()
std::generate(V.begin(), V.end(), B);

where V is just a standard container of elements of the type "type", but it
does NOT necessarly contains instances of the class foo.

The idea is that B is a member function of foo but that the objects in the
container are not of the type foo.
I cannot make B static 'cause it uses members variables of the class foo.

I supposed I need something like std::generator(V.begin(), V.end(),
bind_func(* this, B)) but what exactly?

The third argument can be an object that has a () operator (i.e., function
call operator). Being an object, you can create it with whatever members you
like, which enables you to access anything without the need for any static
function or data. e.g.,

class foo;

class foo_B
{
foo &f;
public:
foo_B(foo &f_) : f(f_) {}
type operator()() { return f.B(); }
};

class foo
{
// data members
private:
friend class foo_B;
void A()
{ std::generate(V.begin(), V.end(), foo_B(*this));
}
type B();
};

DW
 
D

David White

David White said:
class foo;

class foo_B
{
foo &f;
public:
foo_B(foo &f_) : f(f_) {}
type operator()() { return f.B(); }

Sorry, this won't compile. Make it:
type operator()();
};

class foo
{
// data members
private:
friend class foo_B;
void A()
{ std::generate(V.begin(), V.end(), foo_B(*this));
}
type B();
};

type foo_B::eek:perator()() { return f.B(); }

DW
 
T

Tanguy Fautré

David White said:
Sorry, this won't compile. Make it:
type operator()();


type foo_B::eek:perator()() { return f.B(); }

DW


Found what I needed in the Boost Lib :)

std::generate(V.begin(), V.end(), boost::bind(&foo::B, this));


Yours,

Tanguy
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top