如何解决在 C++ 中使用泛型编程的装饰器模式:如何引用模板类参数的嵌套类
我正在创建通用装饰器模式类,在一个装饰器实现中,我试图访问用作模板参数的类的嵌套类,如下所示:
class A
{
public:
class Nested
{
};
};
template<class GenericClass>
class SomeDecoratorImplementation
{
void doSomethingWithNestedClass(GenericClass::Nested& nested) {}
};
为什么这样做:
class A
{
public:
class Nested
{
};
class Listener
{
public:
virtual void somethingChanged() = 0;
};
};
template<class GenericClass>
class SomeDecoratorImplementation : GenericClass::Listener
{
void somethingChanged() override {}
};
int main()
{
SomeDecoratorImplementation<A> test;
}
但不是这个?
class A
{
public:
class Nested
{
};
class Listener
{
public:
virtual void createdNestedClass(Nested& nested) = 0;
};
};
template<class GenericClass>
class SomeDecoratorImplementation : GenericClass::Listener
{
void createdNestedClass(GenericClass::Nested& nested) override {}
};
int main()
{
SomeDecoratorImplementation<A> test;
}
为了进一步简化问题,为什么不编译?对于编译器来说,GenericClass::Listener
类型在逻辑上不应该与 GenericClass::Nested
相同吗?为什么这里可以继承而不能组合?
template<class GenericClass>
class SomeDecoratorImplementation : GenericClass::Listener
{
void createdNestedClass(GenericClass::Nested& nested) {}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。