C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

calling member functions with parameter(s)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Vlad
Guest





PostPosted: Thu Dec 23, 2004 10:13 pm    Post subject: calling member functions with parameter(s) Reply with quote



Dear All,

Here is a made up code:

class A
{
public:
A(int n) : n_(n) {}
void Print() const { cout << n_ << endl; }
void Print(ostream& os) const { os << n_ << endl; }
private:
int n_;
};

int main() {
vector
// ... intialization of v ...

// calls "void A::Print() const" for each element in v
for_each(v.begin(), v.end(), const_mem_fun_t<void, A>(&A::Print));
}

I've been trying to call "void A::Print(ostream&) const" using for_each()
but I don't know how to bind parameter to the Print. Is there a way to do
it or I'm doomed to end up with a regular loop?

So in general the question sounds like: is it possible to avoid using
loops to enumerate through the sequence of objecs and calling their
non-static member function with parameter(s)?

Thank you everybody.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
M Jared Finder
Guest





PostPosted: Fri Dec 24, 2004 9:52 am    Post subject: Re: calling member functions with parameter(s) Reply with quote



Vlad wrote:
Quote:
Dear All,

Here is a made up code:

class A
{
public:
A(int n) : n_(n) {}
void Print() const { cout << n_ << endl; }
void Print(ostream& os) const { os << n_ << endl; }
private:
int n_;
};

int main() {
vector
// ... intialization of v ...

// calls "void A::Print() const" for each element in v
for_each(v.begin(), v.end(), const_mem_fun_t<void, A>(&A::Print));
}

I've been trying to call "void A::Print(ostream&) const" using for_each()
but I don't know how to bind parameter to the Print. Is there a way to do
it or I'm doomed to end up with a regular loop?

So in general the question sounds like: is it possible to avoid using
loops to enumerate through the sequence of objecs and calling their
non-static member function with parameter(s)?

You'll need to create a function object that takes one parameter, the
iterator. The easiest way to do this would be to use Boost.Bind like this:

for_each( v.begin(), v.end(), bind( &A::Print, _1, ref( cout )));

Boost is not part of the C++ standard yet, but I treat it as if it were,
since it is widely supported and very very useful.

-- MJF

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
L.Suresh
Guest





PostPosted: Fri Dec 24, 2004 10:12 am    Post subject: Re: calling member functions with parameter(s) Reply with quote



I think it is not possible pass std::ostream to a member function.
Pleas correct me if i'm wrong.

With ref to the code below..
In the call to bind2nd, the bound second argument is std::ostream.

If second_argument_type of binder2nd

a) is std::ostream, then the bound argument is stored as a copy in the
binder. Since ostreams cannot be copied or assigned this cannot be
done.

b) is std::ostream&, then there might be a reference to reference
problems. Else, the second argument is stored as a reference to
non-const. The constructor to binder2nd takes a reference to const. So
this cannot be done too.

c) is std::ostream const&, still reference to reference problems exist
or the print should take std::ostream const&, which limits its use.

The other alternative i can see is to make the print method take a
pointer to std::ostream, but that renders its usage cumbersome.

--lsu

#include <iostream>
#include <vector>

class A
{
public:
A(int n) : n_(n) {}
void print() const {
std::cout << n_ << std::endl;
}

void print(std::ostream* os) const {
os->operator << (n_); //Cumbersome syntax.
}
private:
int n_;
};


int main() {
std::vector v.push_back(new A(111)); //Deletions ignored!!
v.push_back(new A(222));


// Call void print() const;
std::for_each(v.begin(), v.end(), std::const_mem_fun_t<void,
A>(&A::print));

// Call void print(std::ostream*) const;
std::for_each(v.begin(), v.end(),
std::bind2nd(
std::const_mem_fun1_t<void, A, std::ostream*>(&A::print),
&std::cout
)
);
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alex Vinokur
Guest





PostPosted: Fri Dec 24, 2004 9:24 pm    Post subject: Re: calling member functions with parameter(s) Reply with quote


"Vlad" <unknown (AT) host (DOT) com> wrote

Quote:
Dear All,

Here is a made up code:

class A
{
public:
A(int n) : n_(n) {}
void Print() const { cout << n_ << endl; }
void Print(ostream& os) const { os << n_ << endl; }
private:
int n_;
};

int main() {
vector
// ... intialization of v ...

// calls "void A::Print() const" for each element in v
for_each(v.begin(), v.end(), const_mem_fun_t<void, A>(&A::Print));
}

I've been trying to call "void A::Print(ostream&) const" using for_each()
but I don't know how to bind parameter to the Print. Is there a way to do
it or I'm doomed to end up with a regular loop?

So in general the question sounds like: is it possible to avoid using
loops to enumerate through the sequence of objecs and calling their
non-static member function with parameter(s)?

[snip]


Something like this.

------ foo.cpp : BEGIN ------
#include <vector>
#include <iostream>
using namespace std;

class A
{
private:
int n_;

public:
A(int n) : n_(n) {}
void Print() const { cout << "Print() : " << n_ << endl;
}
void Print(int n_i) const { cout << "Print(int) : " << n_ << " " <<
n_i << endl; }
void Print(ostream* os) const { *os << "Print(ostream*) : " << n_ << endl;
}
~A() { cout << "Dtor" << endl;}

};

int main()
{
vector v.push_back(new A(100));
v.push_back(new A(200));

for_each (v.begin(), v.end(), const_mem_fun_t<void, A>(&A::Print));
for_each (v.begin(), v.end(), bind2nd(const_mem_fun1_t<void, A,
int>(&A::Print), 300));
for_each (v.begin(), v.end(), bind2nd (const_mem_fun1_t<void, A,
ostream*>(&A::Print), &cerr));

for (int i = 0; i < v.size(); i++) delete v[i];
v.clear();

return 0;
}
------ foo.cpp : END --------



------ Compilation & Run : BEGIN ------

// gpp.exe (GCC) 3.4.1

$ gpp foo.cpp

$ a

Print() : 100
Print() : 200
Print(int) : 100 300
Print(int) : 200 300
Print(ostream*) : 100
Print(ostream*) : 200
Dtor
Dtor

------ Compilation & Run : END ------


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maxim Yegorushkin
Guest





PostPosted: Fri Dec 24, 2004 9:25 pm    Post subject: Re: calling member functions with parameter(s) Reply with quote

On 23 Dec 2004 17:13:48 -0500, Vlad <unknown (AT) host (DOT) com> wrote:

Quote:
class A
{
public:
A(int n) : n_(n) {}
void Print() const { cout << n_ << endl; }
void Print(ostream& os) const { os << n_ << endl; }
private:
int n_;
};

int main() {
vector // ... intialization of v ...

// calls "void A::Print() const" for each element in v
for_each(v.begin(), v.end(), const_mem_fun_t<void, A>(&A::Print));
}

I've been trying to call "void A::Print(ostream&) const" using for_each()
but I don't know how to bind parameter to the Print. Is there a way to do
it or I'm doomed to end up with a regular loop?

You can use boost::bind for it:

std::ostream my_stream(...);
for_each(v.begin(), v.end(), boost::bind(&A::Print, _1,
boost::ref(my_stream)));

Quote:
So in general the question sounds like: is it possible to avoid using
loops to enumerate through the sequence of objecs and calling their
non-static member function with parameter(s)?

It is. Please, check out boost::bind docs at
http://www.boost.org/libs/bind/bind.html

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Sat Dec 25, 2004 5:47 pm    Post subject: Re: calling member functions with parameter(s) Reply with quote

the child?s breast into
cutlets, it will be indistinguishable. The taste of young human,
although similar to turkey (and chicken) often can be wildly
different depending upon what he or she has consumed during its
10 to 14 months of life...

4 well chosen cutlets (from the breasts of 2 healthy neonates)
2 large lemons (fresh lemons always, if possible)
Olive oil
Green onions
Salt
pepper
cornstarch
neonate stock (chicken, or turkey stock is fine)
garlic
parsley
fresh cracked black pepper

Season and sauté the cutlets in olive oil till golden brown, remove.
Add the garlic and onions and cook down a bit.
Add some lemon juice and some zest, then de-glaze with stock.
Add a little cornstarch (dissolved in cold water) to the sauce.
You are just about there, Pour the sauce over the cutlets,
top with parsley, lemon slices and cracked pepper.
Serve with spinach salad, macaroni and cheese (homemade) and iced tea...



Spaghetti with Real Italian Meatballs

If you don?t have an expendable bambino on hand,
you can use a pound of ground pork instead.
The secret to great meatballs, is to use very lean meat.

1 lb. ground flesh; human or pork
3 lb. ground beef
1 cup finely chopped onions
7 - 12 cloves garlic
1 cup seasoned bread crumbs
˝ cup milk, 2 eggs
Oregano
basil
salt
pepper
Italian seasoning, etc.
Tomato gravy (see index)
Fresh or at least freshly cooked spaghetti or other pasta

Mix the ground meats together in a large bowl,
then mix each of the other ingredients.
Make balls about the size of a baby?s fist
(there should b


Back to top
Maxim Yegorushkin
Guest





PostPosted: Sat Dec 25, 2004 9:08 pm    Post subject: Re: calling member functions with parameter(s) Reply with quote

flesh, chicken, turkey, beef...)
1 -2 lbs. coarsely chopped vegetables
(carrots, potatoes, turnips, cauliflower, cabbage...)
Bell pepper
onions
garlic
ginger
salt pepper, etc.
Olive oil
butter

Brown the meat and some chopped onions, peppers, and garilic in olive oil,
place in baking dish, layer with vegetables seasoning and butter.
Bake at 325° for 30 - 45 minutes.
Serve with hot dinner rolls, fruit salad and sparkling water.



Bébé Buffet 1

Show off with whole roasted children replete with apples in mouths -
and babies? heads stuffed with wild rice. Or keep it simple with a
hearty main course such as stew, lasagna, or meat loaf.

Some suggestions

Pre-mie pot pies, beef stew, leg of lamb, stuffed chicken, roast pork spiral ham,
Cranberry pineapple salad, sweet potatoes in butter, vegetable platter, tossed salad with tomato and avocado, parsley new potatoes, spinich cucumber salad, fruit salad
Bran muffins, dinner rolls, soft breadsticks, rice pilaf, croissants
Apple cake with rum sauce, frosted banana nut bread sherbet, home made brownies
Iced tea, water, beer, bloody marys, lemonade, coffee



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.