Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符重载<&书信电报;在C++;_C++_Io_Operator Overloading - Fatal编程技术网

C++ 运算符重载<&书信电报;在C++;

C++ 运算符重载<&书信电报;在C++;,c++,io,operator-overloading,C++,Io,Operator Overloading,我写了这个简单的程序来练习重载 这是我的代码: #include <iostream> #include <string> using namespace std; class sex_t { private: char __sex__; public: sex_t(char sex_v = 'M'):__sex__(sex_v) { if (sex_v != 'M' &&

我写了这个简单的程序来练习重载

这是我的代码:

#include <iostream>
#include <string>
using namespace std;

class sex_t
{
    private:
        char __sex__;

    public:
        sex_t(char sex_v = 'M'):__sex__(sex_v)
        {
            if (sex_v != 'M' && sex_v != 'F')
            {
                cerr << "Sex type error!" << sex_v << endl;
                __sex__ = 'M';
            }
        }

        const ostream& operator << (const ostream& stream)
        {
            if (__sex__ == 'M')
                cout << "Male";
            else
                cout << "Female";
            return stream;
        }
};

int main(int argc, char *argv[])
{
    sex_t me('M');
    cout << me << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
阶级性别
{
私人:
字符性;
公众:
sex_t(charsex_v='M'):_sex_(sex_v)
{
如果(sex_v!=“M”&&sex_v!=“F”)
{

cerr运算符的参数和返回值
运算符“C++入门第四版”第14章第14.2节输入和输出运算符:

IO运算符必须是非成员函数,我们不能使运算符成为我们自己类的成员。如果这样做,则左侧操作数必须是我们类类型的对象:

// if operator<< is a member of Sales_item
     Sales_item item;
     item << cout;

//如果运算符在重载流时,您非常严肃地声明了一个
cerr,您绝对不应该做的一件事是使用两个前导下划线。这是为实现保留的。我应该做什么?使用
\u sex\u
或其他方法?类成员的一些常见方法是
m sex
\u sex
sex_
,或者只是
sex
,这取决于你对函数参数的命名。sex、streams、
friend
s访问
private
成员…或者我只是发现无意的影射或者Stroustrup是一个变态。使用const引用的另一个优点是你可以这样做:
std::cout@chris是的:)我差点忘了哪个更好?一个朋友
接线员@shengy,一个
operator@chris我是说一个朋友operator@shengy,啊,通常情况下,如果你能提供一个非友元方法,它会更好,因为它给函数的特权最少。@shengy-我会使用一个普通的
操作符
// if operator<< is a member of Sales_item
     Sales_item item;
     item << cout;
   // general skeleton of the overloaded output operator
     ostream&
     operator <<(ostream& os, const ClassType &object)
     {
         // any special logic to prepare object

         // actual output of members
         os << // ...

         // return ostream object
         return os;
     }
class sex_t
{
private:
    char __sex__;
public:
    sex_t(char sex_v = 'M'):__sex__(sex_v)
    {
        if (sex_v != 'M' && sex_v != 'F')
        {
            cerr << "Sex type error!" << sex_v << endl;
            __sex__ = 'M';
        }
    }

    friend ostream& operator << (ostream& stream, sex_t& sex);
};


ostream& operator << (ostream& stream, sex_t& sex)
{
    if (__sex__ == 'M')
        stream << "Male";
    else
        stream << "Female";
    return stream;
}