开发者

C++ stream second insertion operator

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


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:

  • using different stream types (e.g. XMLStream rather than std::ostream), and defining XMLStream& operator<<(XMLStream&, const My_Type&) etc, and/or
  • using stream manipulators - you can create your own - random Google result: http://www.informit.com/articles/article.aspx?p=171014&seqNum=2


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.


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.


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.


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";


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜