// ConsoleApplication9.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
class A {
public:
A() {
a = 1;
}
//只读函数 只读对象可以调用
int fun() const {
}
//普通函数 只读对象不能调用
int fun2() {
}
private:
int a;
};
class B {
private:
A a;
public:
//返回只读对象的引用
const A& getA() {
return a;
}
};
int main()
{
B b;
// 获取只读对象
const A& a = b.getA();
// 调用只读函数
int a1 = a.fun();
//int a2_1 = a.fun2()//fun2 无法访问,因为a是只读的,只能访问只读的函数
A a2;
int a2_1 = a2.fun2();
std::cout << "Hello World!\n";
}