in C++, can I derive a class from a struct(在 C++ 中,我可以从结构派生一个类吗)
问题描述
问题说明了一切.我是否允许从结构派生一个类,还是应该创建一个嵌入我的结构并定义复制构造函数和 = 运算符以在两者之间移动的类?
The question says it all really. Am I allowed derive a class from a struct, or should I create a class that embeds my struct and defines copy constructors and an = operator to move between the two?
推荐答案
在 C++ 中 struct
(几乎)与 class
同义(除了不同的默认访问级别),所以是的,你可以.
In C++ struct
is (almost) synonymous to a class
(except of different default access level), so yes, you can.
struct A {
// fields are public by default
};
class B: public A {
// fields are private by default
};
我不熟悉 MFC,但它看起来像是在尝试同时维护 C 和 C++ API.
I'm not familiar with MFC, but it looks like an attempt to maintain both C and C++ APIs.
这篇关于在 C++ 中,我可以从结构派生一个类吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中,我可以从结构派生一个类吗


基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01