C++大学基础教程_10_4_friend函数和friend类
2014-07-14 22:51
531 查看
//testMain.cpp #include <iostream> using namespace std; class Count { //使用非friend函数来修改private成员是错误尝试 //用friend函数修改类的private数据 friend void setX(Count &,int); public: Count() :x(0) {}//函数体为空 void print() const { cout << x << endl; } private: int x; }; void setX(Count &c,int val) { c.x = val; } int main() { Count counter; cout << "counter.x after instantiation: " ; counter.print(); setX(counter,8); cout << "counter.x after call to setX friend function: "; counter.print(); system("pause >> cout"); return 0; }
相关文章推荐
- C++大学基础教程_10_10_代理类
- C++大学基础教程_11_10_实例研究:String类
- C++大学基础教程 _10_3_组成:对象作为类的成员
- C++大学基础教程_10_5_使用this指针
- C++大学基础教程_10_67_new和delete和static
- C++基础入门教程(九):函数指针之回调
- 笔记10 《C++ 大学教程》
- MySQL基础教程10 —— 函数之全文搜索功能
- C++大学基础教程_11_8_实例研究:Array类
- 【笨木头C++】基与基础10:又是函数之引用
- C++大学基础教程_12_面向对象编程:继承
- 经典基础教程---Programming TCP/IP Windows Sockets in C++
- C/C++基础教程{100部}
- C++学习(八)函数基础
- [C++基础]重载、覆盖、多态与函数隐藏(1)
- C++基础之const成员函数
- C++基础教程 学习笔记(一) C++的数据类型和修饰符
- C/C++基础知识:函数指针和指针函数的基本概念
- C++基础知识(三)——函数
- [C++基础]重载、覆盖、多态与函数隐藏(2)