[英]How can I make a function only possible to be called by certain subclasses in c++?
I have a class base
with a private array (I cannot make it protected). 我有一个类base
带私人阵列(我不能让它受保护)。 I have getters and setters set for objects in the array. 我为数组中的对象设置了getter和setter方法。 base
has a subclass sub1
and sub1
has a subclass sub2
. base
具有子类sub1
, sub1
具有子类sub2
。
Is there a way to make sub1
only able to access some of base
's getter's and setter's and make sub2
able to access other getters and setters? 有没有一种方法可以使sub1
仅能够访问base
的getter和setter的某些内容,并使sub2
能够访问其他getter和setter的内容?
Something with virtual methods perhaps? 也许有一些虚方法?
No. Not in any good fashion 不,不是很好
If you want this, you should break your base class into better granularity by creating several polymorphic interfaces and instead pass those interfaces around via pointer or reference so that each class only sees the functions it needs to see. 如果需要,可以通过创建多个多态接口将基类分解为更好的粒度,而应通过指针或引用传递这些接口,以便每个类只能看到其需要查看的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.