C ++中可能的封装违规 [英] Possible encapsulation violation in C++

查看:63
本文介绍了C ++中可能的封装违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




B级

{

公开:


虚拟void foo()

{

//一些impl

}

}

D类:公共B

{

私人:


void foo()

{

//一些impl

}

}


void main()

{

B * b =新D();

b-> foo();

}


这个程序运行正常!


基本上,我们可以访问派生类的私有函数

使用基类指针。为什么在C ++中允许它?是不是

封装的消息???


谢谢&问候,

Naresh Agarwal

Hi

Class B
{
public :

virtual void foo()
{
// some impl
}
}

Class D : public B
{
private :

void foo()
{
// some impl
}
}

void main()
{
B *b = new D();
b->foo();
}

This program works fine!

So basically, we are able to access a private function of derived class
using a base class pointer. Why is it allowed in C++? Isn''t it
voilation of encapsulation???

thanks & regards,
Naresh Agarwal

推荐答案




Naresh Agarwal写道:


Naresh Agarwal wrote:
是不是
封装的声音???
Isn''t it
voilation of encapsulation???




没有你所拥有的一切完成了是一种关系。



No all you have done is fuxored the is-a relationship.




Hang Dog写道:

Hang Dog wrote:
Naresh Agarwal写道:
Naresh Agarwal wrote:
是不是
封装的消息???
Isn''t it
voilation of encapsulation???



你所做的一切都是非常有用的-a relationship。



No all you have done is fuxored the is-a relationship.




如果它是关于is-a的关系,那么为什么编译器会给出错误

如果我们分配一个对象D(sy d)在堆栈上并说d.foo()。显然

混淆。

实际上,最终结果表明行为不一致。如果访问

说明符是函数签名的一部分,则此问题不会产生




If it is all about is-a relationship, then why does compiler give error
if we allocate an object of D ( sy d)on stack and say d.foo() . Clearly
its a mix-up.
Effectively the end result points to inconsistent behaviour. If access
specifier was a part of function signature then this problem wouldnt
arise.





chintoo写道:


chintoo wrote:
Hang Dog写道:
Hang Dog wrote:
Naresh Agarwal写道:

Naresh Agarwal wrote:

是不是
封装的消息???

你所做的一切都不仅仅是一种关系。
Isn''t it
voilation of encapsulation???

No all you have done is fuxored the is-a relationship.



如果它是关于is-a的关系,那么为什么编译器会给出错误
如果我们在堆栈上分配D(sy d)的对象并说d.foo( )。显然它是一个混乱。


If it is all about is-a relationship, then why does compiler give error
if we allocate an object of D ( sy d)on stack and say d.foo() . Clearly
its a mix-up.



因为作为D,该方法是私有的,但作​​为B,它是公共的。 B的行为与B一致,D行为与它有关

是D.问题是D不是B而是导出错误。

这是一个设计问题而不是语言问题。


实际上,最终结果表明行为不一致。如果访问
说明符是函数签名的一部分,那么这个问题就不会出现了。


Because as a D the method is private but as a B it is public. The B
behaviour is consitent with B, that D behaviour is consitent with it
being a D. The problem is that D is not a B but derived incorrectly.
This is a design issue not a language issue.

Effectively the end result points to inconsistent behaviour. If access
specifier was a part of function signature then this problem wouldnt
arise.




有很多方法可以让你出现不一致的行为。对于我们的旧类heirarchies中的

示例,我们有一个虚拟重置()

方法。该方法的目的是将实例重置为状态

如果它是默认构造的话。一位同事决定这个

将是一个很好的方法名称,用于在派生类中重置迭代器

。结果不一致的行为和fuxored是-a

的关系。这也是为什么虚拟方法不应公开的原因。



There are many ways in which you can get inconsitent behaviour. For
example in one of our old class heirarchies we have a virtual reset()
method. The intent of the method was to reset the instance to the state
it would be in if it were default constructed. A colleague decide this
would be a nice method name for a function which reset an iterator
within a derived class. Result inconsitent behaviour and fuxored is-a
relationship. This is also an example of why virtual methods should not
be public.


这篇关于C ++中可能的封装违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆