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

6 Answers 6

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:

1
  • Really like this answer, especially the possibility of using different streams! Thanks.
    – Cookie
    Jul 28, 2011 at 7:13
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.

0
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.

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.

2
  • 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 Jul 27, 2011 at 8: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, 2011 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";
1
  • @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, 2011 at 7: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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.