generate

C

Chameleon

I totally messed up with this:

We have
--------------------------------------
generate(v.begin(), v.end(), my_func);
--------------------------------------

and my_func:
-----------
int my_func();
-----------

So, if my_func() does not take parameters, how can generate data?
I see in examples, it uses static variables, but it isn't thread-safe.

I have a class, and I want from data of class produce values to fill
vector v. But in my case my_func() MUST NOT be static. How can I pass a
non-static member function to generate()?

------------------------------------------------
template<class ForwardIterator, class Generator>
void generate(
ForwardIterator _First,
ForwardIterator _Last,
Generator _Gen
);
------------------------------------------------
What is "class Generator"?
Is something like "Interface" in Java?
I must implement a specific method in my class, and my class must derive
from Generator?

thanks and sorry for my bombing (3 questions in a day)
 
A

Alf P. Steinbach

* Chameleon:
I totally messed up with this:

We have
--------------------------------------
generate(v.begin(), v.end(), my_func);
--------------------------------------

and my_func:
-----------
int my_func();
-----------

So, if my_func() does not take parameters, how can generate data?
I see in examples, it uses static variables, but it isn't thread-safe.

I have a class, and I want from data of class produce values to fill
vector v. But in my case my_func() MUST NOT be static. How can I pass a
non-static member function to generate()?

I'd recommend using a simple loop instead of std::generate.

But if you feel you absolutely must use std::generate, pass it a functor
object.

That's an object that provides an operator(); see the FAQ for more
information.
 
V

Victor Bazarov

Chameleon said:
I totally messed up with this:

We have

It could be just 'rand', right?
I see in examples, it uses static variables, but it isn't thread-safe.

No, I suppose not. Threads are not part of the language, though.
I have a class, and I want from data of class produce values to fill
vector v. But in my case my_func() MUST NOT be static. How can I pass a
non-static member function to generate()?

You don't pass a member. You pass an object of your class.
------------------------------------------------
template<class ForwardIterator, class Generator>
void generate(
ForwardIterator _First,
ForwardIterator _Last,
Generator _Gen
);

A functor.
Is something like "Interface" in Java?

No. The whole idea is that it should have data members.
I must implement a specific method in my class, and my class must derive
from Generator?

Yes, you need to implement a function call operator.
thanks and sorry for my bombing (3 questions in a day)

Don't be sorry, the more you ask, the more you learn (hopefully).

BTW, what book on the Standard library are you currently reading that does
not answer those concerns/questions?

V
 
R

Ron Natalie

Chameleon said:
What is "class Generator"?
Is something like "Interface" in Java?
I must implement a specific method in my class, and my class must derive
from Generator?
First off, in a template definition, the word "class" in the argument
just means any type (doesn't necessarily have to be a class).

The function class in the template, doesn't have to be a function.
It can be a object of a class that implements operator(). That
object can keep track in it's own member variables.
 
C

Chameleon

Victor said:
It could be just 'rand', right?


No, I suppose not. Threads are not part of the language, though.


You don't pass a member. You pass an object of your class.


A functor.


No. The whole idea is that it should have data members.


Yes, you need to implement a function call operator.


Don't be sorry, the more you ask, the more you learn (hopefully).

BTW, what book on the Standard library are you currently reading that does
not answer those concerns/questions?

I read Thinking in C++ but about 4 years before and until now, I used
C++ like a C with classes, (multiple) inheritance, string, vector, map
and other goodies. For instance I NEVER used iterators before. Only to
have access in algorithm members like:
sort(v.begin() + index_start, v.begin() + index_end);
 
V

Victor Bazarov

Chameleon said:
[..]
I read Thinking in C++ but about 4 years before and until now, I used
C++ like a C with classes, (multiple) inheritance, string, vector, map
and other goodies. For instance I NEVER used iterators before. Only to
have access in algorithm members like:
sort(v.begin() + index_start, v.begin() + index_end);

Well, that's a good start. Now you need to add your own predicate.
Both a function and a class. Should make you learn a tad.

With all due respect to Bruce Eckel, you need to get yourself a copy
on the Standard Library (Josuttis is the best) and I also recommend
"C++ Templates" by Vandevoorde and Josuttis.

V
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top