C++友元函数(非成员函数)

来源:互联网 发布:数据的完整性 编辑:程序博客网 时间:2024/06/02 04:39
#include <iostream.h>
#include <string>




class Student
{
private:
int age;
public:
Student(){
age=10;
}
out()
{
cout<<age<<endl;
}
//申明友元函数 必须要在类中申明 但是不是类成员函数
friend operator+(int i,Student &t);
};


//函数定义  不用类符号
operator+(int i,Student &t)
{
cout<<i+t.age<<endl;
}
int main()
{
Student t;
4+t;
return 0;
}