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; |