zoukankan      html  css  js  c++  java
  • 左移和右移运算符的重载问题(里面的绝对盲点)在做了一遍,还是出错了

    #include <iostream>
    //实现左移和右移的重载。
    using namespace std;
    
    class A
    {
    public:
    	friend ostream operator<<(ostream &t1,A &a0);
    public:
    	A(int i)
    	{
    		this->a=i;
    	}
    public:
    	int a;
    protected:
    };
    
    ostream operator<<(ostream &t1,A &a0)
    {
    	t1<<a0.a<<endl;
    	return t1;
    }
    int main()
    {
    	A a1(10);
    	cout<<a1;
    	system("pause");
    	return 0;
    }
    

      上面这种是错的,因为定义了类ostream的对象,其他人员是不能改变iostream的任何变量和对象的,所以这里要加一个引用才对呢

    #include <iostream>
    //实现左移和右移的重载。
    using namespace std;
    
    class A
    {
    public:
    	friend ostream &operator<<(ostream &t1,A &a0);
    public:
    	A(int i)
    	{
    		this->a=i;
    	}
    public:
    	int a;
    protected:
    };
    
    ostream &operator<<(ostream &t1,A &a0)
    {
    	t1<<a0.a<<endl;
    	return t1;
    }
    int main()
    {
    	A a1(10);
    	cout<<a1;
    	system("pause");
    	return 0;
    }
    

      

    #include <iostream>
    
    using namespace std;
    
    class sten_fri
    {
    public:
    	sten_fri(int a)
    	{
    		this->a = a;
    	}
    	friend ostream &operator <<(ostream &out, sten_fri sf);
    
    protected:
    private:
    	int a;
    };
    ostream &operator <<(ostream &out, sten_fri sf)
    {
    	out << sf.a << endl;
    	return out;
    }
    
    int main()
    {
    
    	sten_fri a1(5);
    	cout << a1;//这里的友元函数中ostream out中间必须加引用,要不然是错误的。
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    树的一些操作
    线程池的概念
    线程池
    BLOB字段来保存fastreport的报表模板
    D7调用XE2 中间层注意事项
    xe2 datasnap中间层+d7客户端调用
    关于延迟时间的一点智慧

    插件
    phpstorm clone 码云项目到本地 Version Control 不显示
  • 原文地址:https://www.cnblogs.com/xiaochige/p/6591520.html
Copyright © 2011-2022 走看看