C++从外部类继承的嵌套类;不允许使用不完整的类型
C++ nested class which inherits from the outer class; Incomplete type not allowed
在 kotlin 中,有一种设计模式,您可以使用密封类模拟具有关联值的 swift 枚举,并使用嵌套类从中继承
https://medium.com/@da_pacheco/using-kotlins-sealed-class-to-approximate-swift-s-enum-with-associated-data-7e0abac88bbf
例如;斯威夫特有:
enum Barcode {
case UPCA(Int, Int, Int, Int)
case QRCode(String)
}
Kotlin 模仿:
sealed class Barcode {
class UPCA(val system: Int, val manufacturer: Int, val product: Int, val check: Int) : Barcode()
class QRCode(val productCode: String) : Barcode()
}
然后,您可以执行一些操作,例如列出Barcode
并遍历所有内容。
您也可以在 Java 和 C# 中使用此模式...没有"密封类",所以你不能阻止未来的人们扩展列表,但它足够接近并且非常方便。
所以现在我正在尝试在C++中执行此操作,并且收到错误消息"不允许不完整的类型">
class ActionToPerform
{
public:
class ClearItems: public ActionToPerform
{ };
};
这是有一定道理的,因为ActionToPerform
类型在编译器ClearItems
时没有完全声明,但它也是愚蠢和烦人的。我可以将ClearItems
类移出ActionToPerform
但随后我失去了范围/命名空间的好处。
有没有办法解决这个问题? 或者这只是C++固有的限制?
只需转发声明您的派生类:
class ActionToPerform
{
public:
class ClearItems;
};
class ActionToPerform::ClearItems: public ActionToPerform
{ };
相关文章:
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 类中的字符串不命名类型
- 不完整类型错误(E0409、E0070、E0515)
- CRTP 单一实例不完整类型或非文本类型
- 使用具有结构不完整类型错误的模板
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- Xalloc 不命名类型
- (结构名/类名)不命名类型C++
- 是否允许类类型的 std::function 成员变量(不完整类型)?
- MSVC 在不知道类型的情况下评估上下文(和错误)
- 为什么我得到变量"Node"不是类型名称错误
- Visual Studio Code "variable " u8 的 C/C++ 扩展名 " " 不是类型名称"
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 实例化具有不完整类型的类模板格式不正确(如果该类型是在之后定义的)
- C++ 外部类不命名类型
- 有没有办法用不变语法获得当前的类类型?
- 标准容器模板可以用不完整类型实例化吗?
- 什么时候不能用“类”代替“类型名”
- 用Clang编译c++11代码的不完整类型
- 为什么C++不能用 "super class" 类型的右值初始化 "derived class" 类型的变量?