将父级设置为私有级,将祖父母级设置为公共级
Making the parent private and the grandparent public for a class
短篇小说:
可以做吗
class A{};
class B:public virtual A{};
class C:public virtual A,private B{};
即"表明"C是A而不是B,但使其实际上是B而不添加虚拟(和相应的vptrs)?
长话短说:A有几种方法。B又加了一些。有时我想禁止使用其中一个。C有这个目的。这个项目有很多B,很少有C。我不想让B成为C.的子类
是的,这将完全按照您的意愿进行。但考虑另一种选择:公开继承并隐藏不需要的方法:
class A
{
public:
int a() {return 0xaa;}
};
class B: public A
{
public:
int b() {return 0xbb;}
};
class C: public B
{
private:
using B::b; // makes the method called b private
};
...
B().b(); // OK, using method b in class B
C().b(); // error: b is private in class C
C().B::b(); // OK: calling b in base-class (not sure if you want to prevent this)
这将同时适用于虚拟继承和非虚拟继承。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 在C++/Linux中设置单调时钟的一些技巧
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 嵌套在类中时无法设置成员数据
- 需要帮助设置在C++中使用的Potrace
- 如何在自删除后将对象设置为nullptr
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何在Ubuntu中使用cmake设置qt4
- ld:bind_at_load和-bitcode_bundle(Xcode设置ENABLE_bitcode=YES)不能
- 如何在boost beast http请求中设置http头
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何在24位SDL_Surface上设置像素的颜色
- std::设置自定义比较器
- 如何设置一个范围来提取我想要获得的信息
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 通过选项卡的文本设置QTabWidget顺序
- 将特征矩阵的向量设置为0
- 将父级设置为私有级,将祖父母级设置为公共级