这个<<是谁去重载呀?是cout还是string?注:此处的string是标准库的string

解决方案 »

  1.   

    应该是 cout , cout << object
      

  2.   

    嗯,谢谢可是cout怎么知道怎么输出object中的成员变量的值呢?
      

  3.   

    应该由string的实作者重载<<,但是<<操作符号不是string类成员,是全局函数
      

  4.   

    << is overriden by "cout" function!
    Not the string !!
      

  5.   

    Q: 可是cout怎么知道怎么输出object中的成员变量的值呢?A: cout 的确不知道,但 << 知道,可能你还会问,为什么 << 知道,:-) 看来你的所有疑问只有 Bjarne Stroustrup 来解答了,看他的书《The C++ Programming Language》关于 bitset 和 cout 的相关部分。
      

  6.   

    to: mahatma_cn(鱼和胸罩不可兼得) 这个全局函数怎么由别人去用呀,弄点伪代码来瞧瞧to:sgnaw(李逍遥)
    怎么说到关键的地方就不说了?按我的理解来说,<<都应该由要输出的类来重载,并且要做为全局函数来重载,可是我就奇了怪了,没见到string有重载<<的代码
      

  7.   

    由string来重载.msdn的描述.
    Illustrates how to use the string::operator<< Standard Template Library (STL) function in Visual C++.template<class _E, class _TYPE, class _A> inline
    basic_ostream<_E, _TYPE>&operator<<( basic_ostream<_E, _TYPE>& OStream,
                const basic_string<_E, _TYPE, _A>& XString);Note   The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.
    Res
    The operator<< is used to insert a string into an output stream. 
    Example
    // StringInsertion.cpp
    // compile with: /EHsc
    // Illustrates how to use the insertion operator
    // (operator<<) to insert a string into an output
    // stream.
    //
    // Functions:
    //
    //    operator<<   Inserts a string into an output stream.
    //////////////////////////////////////////////////////////////////////#pragma warning(disable:4786)
    #include <string>
    #include <iostream>using namespace std ;int main()
    {
        string msg="Hello!  This is the insertion operator.";
        cout << msg << endl;
    }
    Output
    Hello!  This is the insertion operator.
      

  8.   

    写个简单的重载函数实现的例子(不一定是和C++定义的那样)只是举例.
    ostream& operator<<(ostream cout,string &str)
    {  
       for(int i=0;i<str.lenght()+1;i++)
       cout<<str[i];                        //operator[]也重载过
      return cout;
    }
      

  9.   

    TO:FVV我也看见了,应该是由string来重载,可是重载operator << 需要写成全局函数呀,难道string类里面也是写成全局函数吗?
      

  10.   

    WinMain 及窗口回调 是全局函数吗,它怎么被封装的。看深入浅出的 大挪移