473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Printing a class

If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,

class Foo {
int i;
public:
Foo() : i(0) { }
friend std::ostream& operator<<(std::ostream& o, const Foo& f);
};

However, I have seen people providing a separate public print() method,
then just calling that from operator<<:

class Foo {
int i;
public:
Foo() : i(0) { }
void print(std::ostream& o);
};

std::ostream& operator<<(std::ostream& o, const Foo& f)
{
f.print(o);
return o;
}

Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 19 '07 #1
14 10596
On Feb 19, 10:23 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,

class Foo {
int i;
public:
Foo() : i(0) { }
friend std::ostream& operator<<(std::ostream& o, const Foo& f);
};

However, I have seen people providing a separate public print() method,
then just calling that from operator<<:

class Foo {
int i;
public:
Foo() : i(0) { }
void print(std::ostream& o);
};

std::ostream& operator<<(std::ostream& o, const Foo& f)
{
f.print(o);
return o;
}

Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
For the sake of convention, friend functions are tyically discouraged
as they break true encapsulation. In my opinion friend functions have
always felt like a hack. That is not to say there are appropriate
ways to use friend functions (and necessary times like overriding <<),
but it is always best to use OOP paradigms when possible. Using
print() as a class method maintains class encapsulation.

j0rd4n

Feb 19 '07 #2
On 19 Feb, 16:23, ricec...@gehennom.invalid (Marcus Kwok) wrote:
If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,

class Foo {
int i;
public:
Foo() : i(0) { }
friend std::ostream& operator<<(std::ostream& o, const Foo& f);
};

However, I have seen people providing a separate public print() method,
then just calling that from operator<<:

class Foo {
int i;
public:
Foo() : i(0) { }
void print(std::ostream& o);
};

std::ostream& operator<<(std::ostream& o, const Foo& f)
{
f.print(o);
return o;
}

Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?
I can't see any great benefit in the print function. It adds another
layer of indirection for no gain, avoiding the friend just for the
sake of it. Just doing it to avoid having to use a friend makes no
sense to me. friend is a code smell. That means that whenever you find
yourself using it, you should consider whether your design is correct.
But that is a completely different thing to saying "every time I want
to use a friend, I will mindlessly restructure the code until the
friend has gone away". In general, consider whether the friend really
*is* the best design and be prepared to accept that the amswer might
be "yes".

In this case I think the answer is yes. It's a fairly well known
idiom, necessary because operator<< can't be a member and the
alternative isn't superior in any way.

If you do decide to go with a print member function then it should
probably be a const member function.

Gavin Deane

Feb 19 '07 #3
j0rd4n wrote:
On Feb 19, 10:23 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,

class Foo {
int i;
public:
Foo() : i(0) { }
friend std::ostream& operator<<(std::ostream& o, const Foo& f);
};

However, I have seen people providing a separate public print() method,
then just calling that from operator<<:

class Foo {
int i;
public:
Foo() : i(0) { }
void print(std::ostream& o);
};

std::ostream& operator<<(std::ostream& o, const Foo& f)
{
f.print(o);
return o;
}

Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

For the sake of convention, friend functions are tyically discouraged
Maybe they are discouraged according to the local style/coding styles in
your shop/community. However, that is hardly a general recommendation.
as they break true encapsulation.
And this rational is misleading at best: C++ takes the approach that a class
defines its interface by specifying the meaning of expressions that objects
are part of (e.g., this best explains operator overloading). In particular,
non-member functions that take objects as parameters are part of the class
interface. Friend declarations do for non-members what access specifiers
(public, private, protected) do for members.
In my opinion friend functions have
always felt like a hack. That is not to say there are appropriate
ways to use friend functions (and necessary times like overriding <<),
but it is always best to use OOP paradigms when possible.
Putting OO programming above other programming styles is, again, a decision
that may make sense for your shop/community. It does, however, not really
generalize from there. Which programming style one should opt for very much
depends on the problem at hand and, maybe even more, on the local culture;
programs are meant to communicate intent to other programmers, so the
reading habits of your peers and their expectations heavily influence what
would be good practice for you. That can mean that most of your code ends
up being OO, but it can also mean that allmost all your code ends up being
heavily templated employing generic programming or policy based design.
Using print() as a class method maintains class encapsulation.
Not any better than a friend operator<<. This is what Stroustrup has to say
about friends and encapsulation:

http://www.research.att.com/~bs/bs_faq2.html#friend

Best

Kai-Uwe Bux
Feb 19 '07 #4
Marcus Kwok <ri******@gehennom.invalidwrote:
[public print() method in a class]
Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?
Argh, sorry for the noise. This is answered in the FAQ, 15.8 and 15.9:
http://www.parashift.com/c++-faq-lit....html#faq-15.9

However, Guru Sutter gives the opposite advice, based on James Kanze's
recommendation (but this article is rather old, 1997-03-07):
http://www.gotw.ca/gotw/004.htm
(number 6 in the Solution)

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 19 '07 #5
j0rd4n <jo***********@gmail.comwrote:
For the sake of convention, friend functions are tyically discouraged
as they break true encapsulation. In my opinion friend functions have
always felt like a hack. That is not to say there are appropriate
ways to use friend functions (and necessary times like overriding <<),
but it is always best to use OOP paradigms when possible. Using
print() as a class method maintains class encapsulation.
After I posted, I found this in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-15.9

Regarding your statement about how friend functions break encapsulation,
the FAQ disagrees, and actually goes so far as to say that friends, when
used properly, actually *enhance* encapsulation:
http://www.parashift.com/c++-faq-lit....html#faq-14.2

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 19 '07 #6
Gavin Deane <de*********@hotmail.comwrote:
I can't see any great benefit in the print function. It adds another
layer of indirection for no gain, avoiding the friend just for the
sake of it.
[snip]
In general, consider whether the friend really
*is* the best design and be prepared to accept that the amswer might
be "yes".

In this case I think the answer is yes. It's a fairly well known
idiom, necessary because operator<< can't be a member and the
alternative isn't superior in any way.
Thanks, after doing more research I found it in the FAQ, which agrees
with you. In fact, since I had been doing it this way, I must have
learned it from the FAQ, but forgot it was in there :)
If you do decide to go with a print member function then it should
probably be a const member function.
True, I just forgot when quickly typing up the post.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 19 '07 #7
On 19 Feb, 18:34, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Thanks, after doing more research I found it in the FAQ, which agrees
with you. In fact, since I had been doing it this way, I must have
learned it from the FAQ, but forgot it was in there :)
I forgot it was in the FAQ too, otherwise I could have just pointed
you at it :-)

Gavin Deane

Feb 19 '07 #8
In article <er**********@news-int2.gatech.edu>,
ri******@gehennom.invalid says...
If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,

class Foo {
int i;
public:
Foo() : i(0) { }
friend std::ostream& operator<<(std::ostream& o, const Foo& f);
};

However, I have seen people providing a separate public print() method,
then just calling that from operator<<:

class Foo {
int i;
public:
Foo() : i(0) { }
void print(std::ostream& o);
};

std::ostream& operator<<(std::ostream& o, const Foo& f)
{
f.print(o);
return o;
}

Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?
IMO, you should generally use a mixture of the two: a protected (and
perhaps virtual) member function, and a friend operator<< that uses the
member function.

This is particularly important if you're using inheritance. In that case
the member function is virtual. The friend function can't be virtual,
but it can invoke a virtual member function.

Though at one time I agreed with James Kanze's (old) advice about making
the print function public, I've changed my mind about that part --
having two different (visible) names that do exactly the same thing
isn't usually a good idea IMO. Redundancy is a minor evil, to be avoided
except when it provides real utility to justify itself.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 19 '07 #9
s5n
On Feb 19, 5:23 pm, ricec...@gehennom.invalid (Marcus Kwok) wrote:
If I have some class Foo, my standard way for printing it is to overload
operator<< as a friend, e.g.,
....
Is there any technical reason why this method should be preferred over
the friend operator<<, assuming that I have complete control over Foo?
Shameless plug:

Another alternative is using libs11n, which allows you to non-
intrusively serialize your objects in a number of file formats without
requiring i/ostream operators for every type. Sometimes this is even
useful as a debugging tool, like:

std::map<int,std::list<double m;
... populate m ...
s11nlite::save( m, std::cout );

See: http://s11n.net

(Disclaimer: i wrote that software)

Feb 20 '07 #10
Jerry Coffin <jc*****@taeus.comwrote:
IMO, you should generally use a mixture of the two: a protected (and
perhaps virtual) member function, and a friend operator<< that uses the
member function.

This is particularly important if you're using inheritance. In that case
the member function is virtual. The friend function can't be virtual,
but it can invoke a virtual member function.
Hmm, that does seem like a good compromise. In my work I usually don't
have to use inheritance very often, but for when I do, I will keep this
technique in mind.
Though at one time I agreed with James Kanze's (old) advice about making
the print function public, I've changed my mind about that part --
having two different (visible) names that do exactly the same thing
isn't usually a good idea IMO.
True, the FAQ makes the same argument.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 20 '07 #11
Marcus Kwok <ri******@gehennom.invalidwrote:
Jerry Coffin <jc*****@taeus.comwrote:
>IMO, you should generally use a mixture of the two: a protected (and
perhaps virtual) member function, and a friend operator<< that uses the
member function.

This is particularly important if you're using inheritance. In that case
the member function is virtual. The friend function can't be virtual,
but it can invoke a virtual member function.

Hmm, that does seem like a good compromise. In my work I usually don't
have to use inheritance very often, but for when I do, I will keep this
technique in mind.
I also should have noticed that this technique is a little further down
the page on the FAQ as well :)

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 20 '07 #12
One thing I like to do is to create an "std::string asString() const"
method
that fills a string with the display of your class.

Regards
Liam Herron

Feb 20 '07 #13
In article <11********************@m58g2000cwm.googlegroups.c om>,
li*********@hotmail.com says...
One thing I like to do is to create an "std::string asString() const"
method that fills a string with the display of your class.
I'd do that with operator<< and a stringstream.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 21 '07 #14
On Feb 19, 10:30 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
j0rd4n <jordan.par...@gmail.comwrote:
For the sake of convention, friend functions are tyically discouraged
as they break true encapsulation. In my opinion friend functions have
always felt like a hack. That is not to say there are appropriate
ways to use friend functions (and necessary times like overriding <<),
but it is always best to use OOP paradigms when possible. Using
print() as a class method maintains class encapsulation.

After I posted, I found this in the FAQ:http://www.parashift.com/c++-faq-lit....html#faq-15.9

Regarding your statement about how friend functions break encapsulation,
the FAQ disagrees, and actually goes so far as to say that friends, when
used properly, actually *enhance* encapsulation:http://www.parashift.com/c++-faq-lit....html#faq-14.2

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Kai-Uwe and Marcus,

I stand corrected. Thanks for the clarification.

Feb 21 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
7
by: Dennis C. Drumm | last post by:
I was wondering if someone could suggest were/how I could get started with setting up printing of a collection of text that includes tables from a grid and other supporting information about the...
2
by: Pkant | last post by:
I want to Print a txt file with this code : class Printing { PrintDocument prndoc = new PrintDocument(); PrintPreviewDialog ppd = new PrintPreviewDialog();
0
by: Nigel | last post by:
I successfully create a .NET Component (Visual Basic .NET) that would print, unfortunately when used within a web browser it appears that .NET security doesn't allow you to run code that interacts...
4
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
6
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
1
by: Glenn | last post by:
I am writing a program for field work that will use a receipt printer. I need to be able to adjust the page settings prior to printing depending on how much needs to be printed. I have been able...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.