我应该从一个私人派生类指针转换到它的基类吗? [英] Should I pointer-cast from a private derived class to its base class?

查看:255
本文介绍了我应该从一个私人派生类指针转换到它的基类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从C ++常见问题中找到这个问题


一般来说,




私人派生类的成员函数或朋友,与基类的
关系是
已知,并且从
PrivatelyDer *向上转换为Base *或
PrivatelyDer& to Base&)是安全的;



但是,PrivatelyDer的用户应该
避免这种不安全的转换,因为
基于


如何理解上述词语?我不认为解释正确或准确。



我有一个这样的代码

  class A {
};

B类:private A {
};

int main(){

B * b = new B();
A * a = new A();

a = b; // wrong
a =(A *)b; // right

}


解决方案

从纯粹的机械观点来看,你是对的:对私人基类的转换将工作并产生工作结果。



FAQ的意义是,设计观点一般是错误的。私人继承真的应该是私人的 - 换句话说,即使它可能工作,你不应该知道它会工作,在某些时候它可能会退出工作 - 因为它是正式的实现细节,不是部分的公共接口,他们可以重新实现类而不使用继承。在这一点上,演员不会再工作了(但是因为你使用了演员,编译器可能不会警告你它从你可能不应该做的事情,不能工作的东西)



编辑:是的,演员 必须工作。根据标准的§5.4/ 7:


...以下static_cast和
reinterpret_cast操作
可选地后面跟随一个const_cast
操作)可以使用
转换符号执行显式类型
转换,即使基类
类型不可访问



- 指向派生的
类类型的对象的指针或派生的
类类型的值可以显式转换
指向
明确的基类类型
的指针或引用;


/ p>

I found this from C++FAQ

Generally, No.

From a member function or friend of a privately derived class, the relationship to the base class is known, and the upward conversion from PrivatelyDer* to Base* (or PrivatelyDer& to Base&) is safe; no cast is needed or recommended.

However users of PrivatelyDer should avoid this unsafe conversion, since it is based on a private decision of PrivatelyDer, and is subject to change without notice.

How to understand the above words? I don't think the explanation is correct or accurate.

I have a code like this

class A{
};

class B: private A{
};

int main(){

    B *b = new B();
    A *a = new A();

    a = b;                    //wrong
    a = (A*)b;            //right

}

解决方案

From a purely mechanical viewpoint, you're right: a cast to a private base class will work and produce working results.

The point of the FAQ is that from a design viewpoint it's generally wrong. Private inheritance is really supposed to mean private -- in other words, even though it may work, you're not supposed to know it'll work, and at some point it may quit working -- since it's officially an implementation detail, not part of the public interface, they could re-implement the class without using inheritance. At that point, the cast wouldn't work any more (but because you've used a cast, the compiler probably won't warn you about it having gone from something you probably shouldn't do to something that can't possibly work at all).

Edit: Yes, the cast does necessarily work. According to §5.4/7 of the standard:

... the following static_cast and reinterpret_cast operations (optionally followed by a const_cast operation) may be performed using the cast notation of explicit type conversion, even if the base class type is not accessible:

— a pointer to an object of derived class type or an lvalue of derived class type may be explicitly converted to a pointer or reference to an unambiguous base class type, respectively;

[emphasis added]

这篇关于我应该从一个私人派生类指针转换到它的基类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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