c++ 类的访问权限探讨

1. 在类域外部(非类域中),类对象访问自身接口和成员的限制,示例如下。

#include <stdlib.h>
#include <iostream>

using namespace std;

class fruit
{
public:
	fruit(int id, int count, int price)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:

	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

int main(int argc, char **argv)
{
	fruit FRUIT_1(000, 100, 15);

	cout << "FRUIT_1.m_count" << FRUIT_1.m_count << endl;
	cout << "FRUIT_1.count()" << FRUIT_1.count() << endl;

	cout << "FRUIT_1.m_price" << FRUIT_1.m_price << endl;
	cout << "FRUIT_1.price()" << FRUIT_1.price() << endl;

	cout << "FRUIT_1.m_id" << FRUIT_1.m_id << endl;
	cout << "FRUIT_1.id()" << FRUIT_1.id() << endl;
}
编译时报错,如下,可见在类域外部,类对象只能访问声明为public的接口。


2. 在自身的类域中,类对象访问自身接口和成员的限制,示例如下:

#include <stdlib.h>
#include <iostream>

using namespace std;

class fruit
{
public:
	fruit(int id, int count, int price)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

	// 访问自身类对象的public成员
	int other_count_member(fruit other)
	{
		return other.m_count;
	}

	// 访问自身类对象的protected成员
	int other_price_member(fruit other)
	{
		return other.m_price;
	}

	// 访问自身类类对象的private成员
	int other_id_member(fruit other)
	{
		return other.m_id;
	}

	// 访问自身类对象的public函数
	int other_count_func(fruit other)
	{
		return other.count();
	}

	// 访问自身类对象的protected函数
	int other_price_func(fruit other)
	{
		return other.price();
	}

	// 访问自身类对象的private函数
	int other_id_func(fruit other)
	{
		return other.id();
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:

	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

int main(int argc, char **argv)
{
	fruit FRUIT_1(111, 100, 10);
	fruit FRUIT_2(222, 200, 20);

	cout << "FRUIT_1.other_count_member(FRUIT_2):" << FRUIT_1.other_count_member(FRUIT_2) << endl;
	cout << "FRUIT_1.other_count_func(FRUIT_2):" << FRUIT_1.other_count_func(FRUIT_2) << endl;

	cout << "FRUIT_1.other_price_member(FRUIT_2):" << FRUIT_1.other_price_member(FRUIT_2) << endl;
	cout << "FRUIT_1.other_price_func(FRUIT_2):" << FRUIT_1.other_price_func(FRUIT_2) << endl;

	cout << "FRUIT_1.other_id_member(FRUIT_2):" << FRUIT_1.other_id_member(FRUIT_2) << endl;
	cout << "FRUIT_1.other_id_func(FRUIT_2):" << FRUIT_1.other_id_func(FRUIT_2) << endl;
}

可以正确编译,运行结果如下,可见在自身的类域中,类对象的所有成员和接口都是可见的。


3. 在其他类域(非继承关系)中,类对象访问自身的接口和成员的限制,示例如下

#include <stdlib.h>
#include <iostream>

using namespace std;

class book
{
public:
	book(int count, int price, int id)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:
	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

class fruit
{
public:
	fruit(int id, int count, int price)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

	// 访问其他类对象的public成员
	int other_count_member(book other)
	{
		return other.m_count;
	}
	
	// 访问其他类对象的public函数
	int other_count_func(book other)
	{
		return other.count();
	}

	// 访问其他类对象的protected成员
	int other_price_member(book other)
	{
		return other.m_price;
	}

	// 访问其他类对象的protected函数
	int other_price_func(book other)
	{
		return other.price();
	}

	// 访问其他类类对象的private成员
	int other_id_member(book other)
	{
		return other.m_id;
	}

	// 访问其他类对象的private函数
	int other_id_func(book other)
	{
		return other.id();
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:

	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

int main(int argc, char **argv)
{
	fruit FRUIT_1(111, 100, 10);
	book BOOK_1(222, 200, 20);

	cout << "FRUIT_1.other_count_member(BOOK_1):" << FRUIT_1.other_count_member(BOOK_1) << endl;
	cout << "FRUIT_1.other_count_func(BOOK_1):" << FRUIT_1.other_count_func(BOOK_1) << endl;

	cout << "FRUIT_1.other_price_member(BOOK_1):" << FRUIT_1.other_price_member(BOOK_1) << endl;
	cout << "FRUIT_1.other_price_func(BOOK_1):" << FRUIT_1.other_price_func(BOOK_1) << endl;

	cout << "FRUIT_1.other_id_member(BOOK_1):" << FRUIT_1.other_id_member(BOOK_1) << endl;
	cout << "FRUIT_1.other_id_func(BOOK_1):" << FRUIT_1.other_id_func(BOOK_1) << endl;
}
编译时出错,如下,可见在非继承关系的类中,其他类对象只有public成员或接口是可见的。


4. 在子类的域中,父类对象的成员和接口的访问限制,示例如下

(1)访问自身基类对象的成员

#include <stdlib.h>
#include <iostream>

using namespace std;

class fruit
{
public:
	fruit(int count, int price, int id)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:
	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

class apple:public fruit
{
public:
	apple(int count, int price, int id)
		:fruit (count, price, id)
	{
	}

	// 访问基类的public成员
	int base_public_member()
	{
		return m_count;
	}
	
	// 访问基类的public函数
	int base_public_func()
	{
		return count();
	}

	// 访问基类的protected成员
	int base_protected_member()
	{
		return m_price;
	}

	// 访问基类的protected函数
	int base_protected_func()
	{
		return price();
	}

	// 访问基类的private成员
	int base_private_member()
	{
		return m_id;
	}

	// 访问基类的private函数
	int base_private_func()
	{
		return id();
	}
};

int main(int argc, char **argv)
{
	apple APPLE_1(111, 100, 10);

	cout << "APPLE_1.base_public_member():" << APPLE_1.base_public_member() << endl;
	cout << "APPLE_1.base_public_func():" << APPLE_1.base_public_func() << endl;

	cout << "APPLE_1.base_protected_member():" << APPLE_1.base_protected_member() << endl;
	cout << "APPLE_1.base_protected_func():" << APPLE_1.base_protected_func() << endl;

	cout << "APPLE_1.base_private_member():" << APPLE_1.base_private_member() << endl;
	cout << "APPLE_1.base_private_func():" << APPLE_1.base_private_func() << endl;
}
编译报错,如下,可见默认情况下,自身子类对象只能访问父类的public,protected 成员和接口,private 接口和成员不可见。


(2)访问其他基类对象的成员,示例如下

#include <stdlib.h>
#include <iostream>

using namespace std;

class fruit
{
public:
	fruit(int count, int price, int id)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:
	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

class apple:public fruit
{
public:
	apple(int count, int price, int id)
		:fruit (count, price, id)
	{
	}

	// 访问基类的public成员
	int base_public_member(fruit other)
	{
		return other.m_count;
	}
	
	// 访问基类的public函数
	int base_public_func(fruit other)
	{
		return other.count();
	}

	// 访问基类的protected成员
	int base_protected_member(fruit other)
	{
		return other.m_price;
	}

	// 访问基类的protected函数
	int base_protected_func(fruit other)
	{
		return other.price();
	}

	// 访问基类的private成员
	int base_private_member(fruit other)
	{
		return other.m_id;
	}

	// 访问基类的private函数
	int base_private_func(fruit other)
	{
		return other.id();
	}
};

int main(int argc, char **argv)
{
	fruit FRUIT_1(111, 100, 10);
	apple APPLE_1(222, 200, 20);

	cout << "APPLE_1.base_public_member(fruit FRUIT_1):" << APPLE_1.base_public_member(FRUIT_1) << endl;
	cout << "APPLE_1.base_public_func(fruit FRUIT_1):" << APPLE_1.base_public_func(FRUIT_1) << endl;

	cout << "APPLE_1.base_protected_member(fruit FRUIT_1):" << APPLE_1.base_protected_member(FRUIT_1) << endl;
	cout << "APPLE_1.base_protected_func(fruit FRUIT_1):" << APPLE_1.base_protected_func(FRUIT_1) << endl;

	cout << "APPLE_1.base_private_member(fruit FRUIT_1):" << APPLE_1.base_private_member(FRUIT_1) << endl;
	cout << "APPLE_1.base_private_func(fruit FRUIT_1):" << APPLE_1.base_private_func(FRUIT_1) << endl;
}
编译报错,如下,可见访问基类的对象时,仿佛访问的是陌生类的对象一样,只能访问其 public 的成员和接口。


5. 父类的域中,子类对象的成员和接口访问限制

(1)在父类域中访问子类对象的成员和接口的限制。

#include <stdlib.h>
#include <iostream>

using namespace std;

class fruit;

class apple:public fruit
{
public:
	apple(int count, int price, int id)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:
	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

class fruit
{
public:
	// 访问子类的public成员
	int derived_public_member(apple &other)
	{
		return other.m_count;
	}

	// 访问子类的public函数
	int derived_public_func(apple &other)
	{
		return other.count();
	}

	// 访问子类的protected成员
	int derived_protected_member(apple &other)
	{
		return other.m_price;
	}

	// 访问子类的protected函数
	int derived_protected_func(apple &other)
	{
		return other.price();
	}

	// 访问子类的private成员
	int derived_private_member(apple &other)
	{
		return other.m_id;
	}

	// 访问子类的private函数
	int derived_private_func(apple &other)
	{
		return other.id();
	}
};

int main(int argc, char **argv)
{
	fruit FRUIT_1;
	apple APPLE_1(222, 200, 20);

	cout << "FRUIT_1.derived_public_member(fruit FRUIT_1):" << FRUIT_1.derived_public_member(APPLE_1) << endl;
	cout << "FRUIT_1.derived_public_func(fruit FRUIT_1):" << FRUIT_1.derived_public_func(APPLE_1) << endl;

	cout << "FRUIT_1.derived_protected_member(fruit FRUIT_1):" << FRUIT_1.derived_protected_member(APPLE_1) << endl;
	cout << "FRUIT_1.derived_protected_func(fruit FRUIT_1):" << FRUIT_1.derived_protected_func(APPLE_1) << endl;

	cout << "FRUIT_1.derived_private_member(fruit FRUIT_1):" << FRUIT_1.derived_private_member(APPLE_1) << endl;
	cout << "FRUIT_1.derived_private_func(fruit FRUIT_1):" << FRUIT_1.derived_private_func(APPLE_1) << endl;
}

编译出错,可见父类域中访问子类对象的接口,如同陌生类一样,非public接口均不可访问。


(2)父类域中,仅仅访问子类对象的public接口,能否成功呢?如下

#include <stdlib.h>
#include <iostream>

using namespace std;

class fruit;

class apple:public fruit
{
public:
	apple(int count, int price, int id)
		:m_count(count), m_price(price), m_id(id)
	{
	}

	int count()
	{
		return m_count;
	}

public:
	int m_count;

protected:
	int price()
	{
		return m_price;
	}

protected:
	int m_price;

private:
	int id()
	{
		return m_id;
	}

private:
	int m_id;
};

class fruit
{
public:
	// 访问子类的public成员
	int derived_public_member(apple &other)
	{
		return other.m_count;
	}

	// 访问子类的public函数
	int derived_public_func(apple &other)
	{
		return other.count();
	}

	 访问子类的protected成员
	//int derived_protected_member(apple &other)
	//{
	//	return other.m_price;
	//}

	 访问子类的protected函数
	//int derived_protected_func(apple &other)
	//{
	//	return other.price();
	//}

	 访问子类的private成员
	//int derived_private_member(apple &other)
	//{
	//	return other.m_id;
	//}

	 访问子类的private函数
	//int derived_private_func(apple &other)
	//{
	//	return other.id();
	//}
};

int main(int argc, char **argv)
{
	fruit FRUIT_1;
	apple APPLE_1(222, 200, 20);

	cout << "FRUIT_1.derived_public_member(fruit FRUIT_1):" << FRUIT_1.derived_public_member(APPLE_1) << endl;
	cout << "FRUIT_1.derived_public_func(fruit FRUIT_1):" << FRUIT_1.derived_public_func(APPLE_1) << endl;

	//cout << "FRUIT_1.derived_protected_member(fruit FRUIT_1):" << FRUIT_1.derived_protected_member(APPLE_1) << endl;
	//cout << "FRUIT_1.derived_protected_func(fruit FRUIT_1):" << FRUIT_1.derived_protected_func(APPLE_1) << endl;

	//cout << "FRUIT_1.derived_private_member(fruit FRUIT_1):" << FRUIT_1.derived_private_member(APPLE_1) << endl;
	//cout << "FRUIT_1.derived_private_func(fruit FRUIT_1):" << FRUIT_1.derived_private_func(APPLE_1) << endl;
}
编译报错,说fruit类不完全。看来,要在父类中调用子类的接口是不太可能的了,因为两者是继承关系,父类要调用子类的接口,必须先定义子类,但是子类的定义依赖父类的定义,所以这样就陷入类似死锁的情况。不过这种需求也是没有必要的。



总结一下:

(1)类域中,可以访问自身对象和此类的外部对象的所有成员和接口。

(2)继承关系的类域中,子类只能访问自身对象的基类的 public 和 protected 成员和接口

(3)继承关系的类域中,子类仅能访问其他的基类对象的 public 成员和接口

(4)继承关系的类域和,父类不能访问任何子类的成员和接口

(5)非继承关系的类域 以及 非类域中,仅能访问类对象的public成员和接口。


后续有新的发现再更新

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值