#include <iostream> using namespace std; class Student{ public: Student(char *name, int age, float score); public: friend void show(Student *pstu); //将show()声明为友元函数 private: char *m_name; int m_age; float m_score; }; Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ } //非成员函数 void show(Student *pstu){ cout<<pstu->m_name<<"的年龄是 "<<pstu->m_age<<",成绩是 "<<pstu->m_score<<endl; } int main(){ Student stu("小明", 15, 90.6); show(&stu); //调用友元函数 Student *pstu = new Student("李磊", 16, 80.5); show(pstu); //调用友元函数 return 0; }运行结果:
void show(){ cout<<m_name<<"的年龄是 "<<m_age<<",成绩是 "<<m_score<<endl; }成员函数在调用时会隐式地增加 this 指针,指向调用它的对象,从而使用该对象的成员;而 show() 是非成员函数,没有 this 指针,编译器不知道使用哪个对象的成员,要想明确这一点,就必须通过参数传递对象(可以直接传递对象,也可以传递对象指针或对象引用),并在访问成员时指明对象。
#include <iostream> using namespace std; class Address; //提前声明Address类 //声明Student类 class Student{ public: Student(char *name, int age, float score); public: void show(Address *addr); private: char *m_name; int m_age; float m_score; }; //声明Address类 class Address{ private: char *m_province; //省份 char *m_city; //城市 char *m_district; //区(市区) public: Address(char *province, char *city, char *district); //将Student类中的成员函数show()声明为友元函数 friend void Student::show(Address *addr); }; //实现Student类 Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ } void Student::show(Address *addr){ cout<<m_name<<"的年龄是 "<<m_age<<",成绩是 "<<m_score<<endl; cout<<"家庭住址:"<<addr->m_province<<"省"<<addr->m_city<<"市"<<addr->m_district<<"区"<<endl; } //实现Address类 Address::Address(char *province, char *city, char *district){ m_province = province; m_city = city; m_district = district; } int main(){ Student stu("小明", 16, 95.5f); Address addr("陕西", "西安", "雁塔"); stu.show(&addr); Student *pstu = new Student("李磊", 16, 80.5); Address *paddr = new Address("河北", "衡水", "桃城"); pstu -> show(paddr); return 0; }运行结果:
'Address' has not been declared
。类的提前声明和函数的提前声明是一个道理。Address addr; //企图使用不完整的类来创建对象
因为创建对象时要为对象分配内存,在正式声明类之前,编译器无法确定应该为对象分配多大的内存。编译器只有在“见到”类的正式声明后(其实是见到成员变量),才能确定应该为对象预留多大的内存。在对一个类作了提前声明后,可以用该类的名字去定义指向该类型对象的指针变量(本例就定义了 Address 类的指针变量)或引用变量(后续会介绍引用),因为指针变量和引用变量本身的大小是固定的,与它所指向的数据的大小无关。#include <iostream> using namespace std; class Address; //提前声明Address类 //声明Student类 class Student{ public: Student(char *name, int age, float score); public: void show(Address *addr); private: char *m_name; int m_age; float m_score; }; //声明Address类 class Address{ public: Address(char *province, char *city, char *district); public: //将Student类声明为Address类的友元类 friend class Student; private: char *m_province; //省份 char *m_city; //城市 char *m_district; //区(市区) }; //实现Student类 Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ } void Student::show(Address *addr){ cout<<m_name<<"的年龄是 "<<m_age<<",成绩是 "<<m_score<<endl; cout<<"家庭住址:"<<addr->m_province<<"省"<<addr->m_city<<"市"<<addr->m_district<<"区"<<endl; } //实现Address类 Address::Address(char *province, char *city, char *district){ m_province = province; m_city = city; m_district = district; } int main(){ Student stu("小明", 16, 95.5f); Address addr("陕西", "西安", "雁塔"); stu.show(&addr); Student *pstu = new Student("李磊", 16, 80.5); Address *paddr = new Address("河北", "衡水", "桃城"); pstu -> show(paddr); return 0; }第 24 行代码将 Student 类声明为 Address 类的友元类,声明语句为:
friend class Student;
有的编译器也可以不写 class 关键字,不过为了增强兼容性还是建议写上。
本文链接:http://task.lmcjl.com/news/8512.html