2

Is it possible to define a second insertion operator to have two modes of outputting a class? Say e.g. one that outputs all members and one that just outputs some basic unique identifier that is grep-able in a log? If so, is there an operator that is usually chosen? I would guess as analogy to << one might use <<< if that is legal?

Thanks

Cookie
  • 12,004
  • 13
  • 54
  • 83

6 Answers6

3

There's no such thing already defined or in use by convention.

Also, you cannot define your own operators in C++, you have to use one of the ones already in the language and overloadable, and <<< isn't an operator in C++, so it is out anyway.

I'd strongly recommend you don't use some other operator for this. (See rule #1 here for a more thorough explanation.) If you have subtle differences between output operations, well-chosen functions names go a long way for making better code than unclear operators arbitrarily picked.

Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
3

If you want to output only the id, then the best idea is probably to provide a method to get the id in a type that's streamable (e.g. std::string id() const;). That's much more intuitive to other people working on the code than some strange operator use.

Your suggestion of <<< (it's not possible to create new operators in C++, but ignoring that for a moment) reveals that you're happy for there to be different code at the point of call. Therefore, the only benefit you'd get would be the saving of a few character's source code; it isn't worth the obfuscation.

By way of contrast, there are situations where you want the same streaming notation to invoke different behaviours, such as switching between id-only and full data, or different representations such as tag/value, CSV, XML, and binary. These alternatives are usually best communicated by either:

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
2

No. You can't define your own operators (<<< doesn't exist in C++). But you can define a id() method returning a string and output this.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
2

There is no such operator as <<< in C++.

You are, however, free to implement, for example operator <(ostream&,Object&), which would do what you want. The problem is, code may get unreadable when you try to chain < and << together.

x13n
  • 4,103
  • 2
  • 21
  • 28
  • 1
    While this is technically true, you can do it, other developers and yourself down the road will appreciate if when overloading operators you use some *sane* semantics. A few months from now you might start wondering how can an `ostream` be smaller than your `object` – David Rodríguez - dribeas Jul 27 '11 at 08:49
  • Well, true. But if he really wants to have another operator for this purpose - that is how it can be achieved. – x13n Jul 27 '11 at 12:12
1

you can use operator | for instance. Another way of doing this is to define small tag classes for which the operator is overloaded; example (pretty simplistic but you get the point):

template< class T >
struct GrepTag
{
  GrepTag( const T& );
  T value;
}

template< class T >
Greptag< T > MakeGrepTag( const T& x )
{
  return GrepTag< T >( x ); 
}

template< class T >
MyClass& MyClass::operator << ( const GrepTag< T >& g )
{
  //output g.value here
}

MyClass() << MakeGrepTag( "text" );

Yet another way, more like the standard streams, is to use a tag as well but keep some state internally:

struct GrepTag
{
}

MyClass& MyClass::operator << ( const GrepTag& g )
{
  grepState = true;
}

template< class T >
MyClass& MyClass::operator << ( const T& )
{
  if( grepState )
  {
    //output special
    grepState = false;
  }
  else
  {
    //output normal
  }        
}

MyClass() << GrepTag() << "text";
stijn
  • 34,664
  • 13
  • 111
  • 163
  • @Alexandre: I removed some typos now; what exactly are you referring to? I'm not showing full class definitions so it won't compile, I know that. – stijn Jul 27 '11 at 07:43
0

You cannot define your own operators in C++. You can only overload those that exist.

So I recomend not using an operator for outputting basic unique identifier grep-able in a log. This doesn't correspond to any existing operator role. Use a method instead, such as exportToLog().

Shlublu
  • 10,917
  • 4
  • 51
  • 70