来自C ++ / Stroustrup的dynamic_cast疑问:转换为受保护的基类 [英] dynamic_cast doubt from C++/Stroustrup : converting to protected base class

查看:164
本文介绍了来自C ++ / Stroustrup的dynamic_cast疑问:转换为受保护的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道下面的代码给出编译错误:

  class A {public:virtual void name(){cout< ; typeid(this).name()<< endl;}; }; 
class B:protected A {public:virtual void name(){cout<< typeid(this).name()<< endl;};};
void foo(B * b)
{
A * a = dynamic_cast< A *>(b); //错误:'A'是'B'的不可访问的基础
return;但是那么为什么在C ++ Stroustrup的书(15.4.1)中,他写的是: / p>

  class BB_ival_slider:public Ival_slider,protected BBslider {// ... 
};
void f(BB_ival_slider * p)
{
// ok
BBslider * pbb2 = dynamic_cast< BBslider *>(p); //确定:pbb2成为0
}

行不应该是编译错误吗?
所以,我的gcc是错误的标记它作为编译错误或不可思议的,stroustrup打字错误或最合理的我错过了一些...

解决方案

15.4.1的实际报价是:


  class BB_ival_slider: public Ival_slider,protected BBslider {
// ...
};

void f(BB_ival_slider * p)
{
Ival_slider * pi1 = p; // ok
Ival_slider * pi2 = dynamic_cast< Ival_slider *>(p); // ok
BBslider * pbb1 = p; //错误:BBslider是受保护的基础
BBslider * pbb2 = dynamic_cast< BBslider *>(p); // ok:pbb2 become 0
}

这是无趣的情况。但是,很高兴知道 dynamic_cast 不允许意外违反对私有和受保护的基类的保护


所以看起来描述代码的文本是正确的,但是出于错误的原因 - dynamic_cast 不会允许意外违反私有和受保护的基类的保护,但只是因为使用它将是不成形的,并会导致编译器错误,而不是因为使用它会产生一个空指针。当然,文字描述的代码 在中正确。



错误发生了 - 也许它将被固定在本书的第4版。 : - ]



(另请注意,如果 BB_ival_slider 声明 f 成为朋友,那么代码的行为如本书所述,也许这 code>声明是在本章前面的暗示,但我没有时间仔细阅读它仔细检查一种方式或其他。)


I know that following code gives compilation error :

class A{ public : virtual void name(){cout<<typeid(this).name()<<endl;}; };
class B:protected A{public : virtual void name(){cout<<typeid(this).name()<<endl;};};
void foo(B* b)
{
    A * a = dynamic_cast<A*>(b); //Error : 'A' is an inaccessible base of 'B'    
    return;
}

But then why in the C++ Stroustrup book (15.4.1) he writes

class BB_ival_slider:public Ival_slider,protected BBslider{ //...
};
void f(BB_ival_slider*p)
{  
// ok 
BBslider* pbb2 = dynamic_cast<BBslider*>(p);    // ok: pbb2 becomes 0
}

Shouldn't the line be compilation error ? So either my gcc is wrong in flagging it as compilation error OR the unthinkable, stroustrup typo or most plausibly I have missed something...

解决方案

The actual quote from 15.4.1 is:

class BB_ival_slider : public Ival_slider, protected BBslider {
    // ...
};

void f(BB_ival_slider* p)
{
    Ival_slider* pi1 = p; // ok
    Ival_slider* pi2 = dynamic_cast<Ival_slider*>(p); // ok
    BBslider* pbb1 = p; // error: BBslider is a protected base
    BBslider* pbb2 = dynamic_cast<BBslider*>(p); // ok: pbb2 becomes 0
}

That is the uninteresting case. However, it is reassuring to know that dynamic_cast doesn't allow accidental violation of the protection of private and protected base classes.

So it would seem that the text describing the code is correct, but for the wrong reasons -- dynamic_cast doesn't allow accidental violation of the protection of private and protected base classes, but only because using it would be ill-formed and will result in a compiler error, not because using it will yield a null-pointer. And, of course, the code the text is describing is definitely incorrect.

Mistakes happen -- maybe it will be fixed in the 4th edition of the book. :-]

(Also, note that if BB_ival_slider declares f to be a friend, then the code will behave as described in the book. Perhaps this friend declaration was implied earlier in the chapter, but I don't have time right now to read over it carefully to check one way or the other.)

这篇关于来自C ++ / Stroustrup的dynamic_cast疑问:转换为受保护的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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