Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading operator << design question

Reply
Thread Tools

overloading operator << design question

 
 
Christopher
Guest
Posts: n/a
 
      09-09-2011
Currently, the code I am fixing seems to have several different global
functions for each type, is written c-style, to convert different
types into 3 different text representations.

ie.
typedef int Distance;
std::string FormatForLogA(Distnace distance);
std::string FormatForLogB(Distance distance);
std::string FormatForLogC(Distance distance);

I like to overload operator << for a type when I am going to be
converting it into a text representation.

Problem here is that there are 3 different text representations for
each type!
Suppose we are dealing with a distance type...
Log A might expect something like "204"
Log B might expect something like "2m"
Log C might expect something like "204 centimeters"

How would you go about diffientatiing which format you want and still
use the idea of overloading op << ?
Can we even do this for those types that are just a typedef of a
primitive without altering the way the primitive type looks in a
stream?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-09-2011
On 9/9/2011 1:04 PM, Christopher wrote:
> Currently, the code I am fixing seems to have several different global
> functions for each type, is written c-style, to convert different
> types into 3 different text representations.
>
> ie.
> typedef int Distance;
> std::string FormatForLogA(Distnace distance);
> std::string FormatForLogB(Distance distance);
> std::string FormatForLogC(Distance distance);
>
> I like to overload operator<< for a type when I am going to be
> converting it into a text representation.
>
> Problem here is that there are 3 different text representations for
> each type!
> Suppose we are dealing with a distance type...
> Log A might expect something like "204"
> Log B might expect something like "2m"
> Log C might expect something like "204 centimeters"


It's unclear whether "Log A" and others will be represented by different
types in your program, or they are all serviced by the same type for
which you're overloading the op<<.

> How would you go about diffientatiing which format you want and still
> use the idea of overloading op<< ?
> Can we even do this for those types that are just a typedef of a
> primitive without altering the way the primitive type looks in a
> stream?


The usual way to go about it, I think, is to have some kind of state in
the stream (or the object for whose type you'll overload the op<<) and
define the manipulators that would set those states. Take a look at the
ostream and its 'fixed' manipulator, for instance.

MyStream streamForLogA;
streamForLogA << useLogA << distance;
MyStream streamForLogB;
streamForLogB << useLogB << distance;

Of course, in that case it would probably be better to set those states
during initialization of the "stream":

MyStream streamForLogA(MyStream::useLogA); // 'useLogA' is an enum

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Kalle Olavi Niemitalo
Guest
Posts: n/a
 
      09-09-2011
Christopher <(E-Mail Removed)> writes:

> Log A might expect something like "204"
> Log B might expect something like "2m"
> Log C might expect something like "204 centimeters"
>
> How would you go about diffientatiing which format you want and still
> use the idea of overloading op << ?
>
> Can we even do this for those types that are just a typedef of a
> primitive without altering the way the primitive type looks in a
> stream?


No, overloading cannot distinguish typedef names of the same type
from each other, and it can't be done at run time either.

If you are going to keep Distance as a typedef for int, you could
wrap the distance in a class for output. For example, define
class Centimeters with a suitable constructor, overload operator
<<(std:stream &, Centimeters), and then use logC <<
Centimeters(distance). This looks much like calling a function
that returns std::string but is in principle more flexible
because the operator << can change the output according to the
properties of the stream. In particular, it could select the
"centimeters" string based on the locale with which the stream
has been imbued.

If you are changing Distance to a struct or class too, then you
can instead store the preferred format in each ostream object.
There are std::ios_base:alloc, std::ios_base::iword, and
std::ios_base:word for this purpose. You'd first call
SetDistanceFormat(logC, DistanceFormatCentimetersWithUnit)
and then just write the distances with logC << distance.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments