boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++小插曲
相关主题
A C++ compiler related interview question
我有个很傻的问题,关于function call via pointer
An interesting C++ compile error
问个虚函数的作用
c++ dynamic cast
再问C++问题。
c++ define 一问
码工试题 (转载)
inline到底能省多少时间?
what's wrong with this C++ code?
相关话题的讨论汇总
话题: foo话题: compiler话题: class话题: private话题: virtual
进入Programming版参与讨论
1 (共1页)
T******r
发帖数: 257
1
class B
{
public:
virtual void foo(){...};
...
};
class D : public B
{
private:
virtual void foo(){...};
...
};
B* p = new D();
p->foo();
偶然发现这样居然也行, D的foo被called.
s***e
发帖数: 122
2
你是说 B* p = new D(); p->foo(); 吧?

【在 T******r 的大作中提到】
: class B
: {
: public:
: virtual void foo(){...};
: ...
: };
: class D : public B
: {
: private:
: virtual void foo(){...};

T******r
发帖数: 257
3
对对. 打错了. 呵呵

【在 s***e 的大作中提到】
: 你是说 B* p = new D(); p->foo(); 吧?
s***e
发帖数: 122
4
确实挺有趣的,D只能new,而其它方法只能通过B的指针调用。有点强制实施Factory模
式的意思。

【在 T******r 的大作中提到】
: 对对. 打错了. 呵呵
r****t
发帖数: 10904
5
我不懂,但是叫 virtual 的method, 就是这么用的吧?倒是直接 call 了 private member, 看起来不是很对,这个里面有什么道理?

【在 T******r 的大作中提到】
: class B
: {
: public:
: virtual void foo(){...};
: ...
: };
: class D : public B
: {
: private:
: virtual void foo(){...};

K*****n
发帖数: 65
6
This means Compiler is not smart enough.
When Compiler enforces access rights, it does not go that far to realize
that p really points to a D object.
By the time the compiler creates vtable, it assumes access rights
enforcement has already accomplished in the previous stage, and
places no more check on access rights. That is why private D::foo is called.
r****t
发帖数: 10904
7
那算是一 compiler 的 bug 了?

called.

【在 K*****n 的大作中提到】
: This means Compiler is not smart enough.
: When Compiler enforces access rights, it does not go that far to realize
: that p really points to a D object.
: By the time the compiler creates vtable, it assumes access rights
: enforcement has already accomplished in the previous stage, and
: places no more check on access rights. That is why private D::foo is called.

K*****n
发帖数: 65
8
I would not call it is a bug. It is an unfortunate reality we have to live
with.
Pointer is powerful and risky. Be cautious when you cast a pointer. Compiler
can not prevent abuse of pointer. For example, you
can get class private data by casting pointer.
Back to the original post, it is a bad programming practice to declare a
public virtual function in base class and to override it as private in
derived class. To make Compiler happy is just not enough. I give you
another example:
class A
{
..
x****u
发帖数: 44466
9
这个是完全符合C++语法的啊,如果不能调用,那才是问题呢。
public和private只是编译阶段的检查而已,你只要骗过了它,那就万事大吉了。从某
种意义上说,这么做可以在某些场合下限制让你只使用基类指针啊。

【在 T******r 的大作中提到】
: class B
: {
: public:
: virtual void foo(){...};
: ...
: };
: class D : public B
: {
: private:
: virtual void foo(){...};

z***e
发帖数: 5393
10
?这有什么问题?
p->foo() 相当于 p->vptr[1](),本来就是放的这个地址。

【在 T******r 的大作中提到】
: class B
: {
: public:
: virtual void foo(){...};
: ...
: };
: class D : public B
: {
: private:
: virtual void foo(){...};

l**a
发帖数: 423
11
如果是object, derived object 被call;
1 (共1页)
进入Programming版参与讨论
相关主题
what's wrong with this C++ code?
C++ interdependence question
ask a simple question about int pointer.
请教 C++的一个困惑 (operator delete)
纯虚函数问题
[合集] 也说个C++面题
请问c++为什么会编译失败?
C++ class library
template 类的继承问题
问个virtual table 的问题
相关话题的讨论汇总
话题: foo话题: compiler话题: class话题: private话题: virtual