本文介绍了问:继承设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 假设我有一个基类和一个派生类,如下所示: class base { public: void somefunc(); }; 派生类:私人基地 { public: void someotherfunc(); }; 我打算阻止''derived''的用户访问 ''base''的成员。问题是,没有什么能阻止用户将'b $ b''派生''类下放到''base''类,然后访问该成员。除了简单记录之外,还有一个 的解决方案吗? 谢谢! - jb (如果你想通过电子邮件回复,用x替换y) 解决方案 " Jakob Bieling" <是ne ***** @ gmy.net>在消息新闻中写道:bm ************* @ news.t-online.com ... 我的打算是阻止''derived''的用户访问''base''的成员。问题是,没有什么能阻止用户将派生类转储到基础类,然后访问该成员。除了简单地记录它之外,还有解决方案吗? 向下转换为base的唯一方法(安全地)是使用C风格的转换。那个'C $风格的演员将扮演的C ++演员不会这样做。为了通过 C ++强制转换(甚至隐式转换),基类需要是可访问的(即, 不是私有的)。 reinterpret_cast是危险的(不能保证转换后的 指针是正确的,并且在多重继承的情况下,它几乎可以肯定是某些错误的。基础)。 Jakob Bieling写道: 假设我有一个基地class和派生类如下: class base { public: void somefunc(); }; 类派生:私人基地公开: void someotherfunc(); }; 我打算阻止用户' '衍生''访问''base''的成员。 为什么?继承的整个想法是''派生''可以像 a''base''一样使用并共享其界面。如果你需要另一个界面, 考虑派生一个基础成员。 问题是没有什么能阻止用户下传''派生''类到''base''类然后访问该成员。是否有解决方案,除了简单记录它? 再次,派生一个基地的成员。 Rolf Magnus写道: Jakob Bieling写道: class base { public: void somefunc(); 派生类:私人基地 {公开: void someotherfunc(); }; 我的打算防止''derived''的用户访问''base''的成员。 为什么?继承的整个想法是''派生''可以像''base'一样使用并共享它的界面。如果你需要另一个界面,考虑派生一个基础成员。 问题是没有什么能阻止用户下传''派生''类到''base''类然后访问该成员。是否有解决方案,除了简单记录它? 再次,使派生成为基础成员。 Grmbl。在发送 之前,我已经习惯了不再阅读我的帖子,这不是一个好主意。当然我的意思是让基地成员 派生。 Hi,Suppose I have a base class and a derived class as follows:class base{public:void somefunc ();};class derived : private base{public:void someotherfunc ();};My intend is to prevent the user of ''derived'' to access the members of''base''. The problem is that nothing prevents the user to down-cast the''derived'' class to the ''base'' class and then access that member. Is there asolution to this, other than simply documenting it?thanks!--jb(replace y with x if you want to reply by e-mail) 解决方案"Jakob Bieling" <ne*****@gmy.net> wrote in message news:bm*************@news.t-online.com... My intend is to prevent the user of ''derived'' to access the members of ''base''. The problem is that nothing prevents the user to down-cast the ''derived'' class to the ''base'' class and then access that member. Is there a solution to this, other than simply documenting it?The only way (safely) to down convert derived to base is to use a C-style cast. That''sthe one cast the C-style cast will do that the C++ casts won''t. In order to cast via aC++ cast (or even implicitly convert) the base class would need to be accessible (i.e.,not private). A reinterpret_cast is dangerous (there''s no guarantee that the convertedpointer would be correct, and in the case of multiple inheritance it almost assuredly iswrong for some of the bases).Jakob Bieling wrote: Hi, Suppose I have a base class and a derived class as follows: class base { public: void somefunc (); }; class derived : private base { public: void someotherfunc (); }; My intend is to prevent the user of ''derived'' to access the members of ''base''.Why? The whole idea of inheritance is that a ''derived'' can be used likea ''base'' and shares its interface. If you need another interface,consider making derived a member of base. The problem is that nothing prevents the user to down-cast the ''derived'' class to the ''base'' class and then access that member. Is there a solution to this, other than simply documenting it?Again, make derived a member of base. Rolf Magnus wrote: Jakob Bieling wrote: Hi, Suppose I have a base class and a derived class as follows: class base { public: void somefunc (); }; class derived : private base { public: void someotherfunc (); }; My intend is to prevent the user of ''derived'' to access the members of ''base''. Why? The whole idea of inheritance is that a ''derived'' can be used like a ''base'' and shares its interface. If you need another interface, consider making derived a member of base. The problem is that nothing prevents the user to down-cast the ''derived'' class to the ''base'' class and then access that member. Is there a solution to this, other than simply documenting it? Again, make derived a member of base.Grmbl. I just got used to not reading my postings anymore before sendingthem, which is not a good idea. Of course I mean making base a memberof derived. 这篇关于问:继承设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 08:15