I've found what appears to be a strange method to break encapsulation of a class derived within a class
take the following
class Base
{
private:
class Member
{
public:
virtual ~Member() {}
virtual void Function() = 0;
};
};
if you try to derive from this private class
class DerivedMember : public Base::Member
{
public:
DerivedMember() {}
virtual void Function() {/*Do something*/}
};
you get a compile error
error: 'class Base::Member' is private
error: within this context
however if you derive from it a template
template <typename T>
class TemplateDerivedMember : public Base::Member
{
public:
TemplateDerivedMember () {}
virtual void Function() {/*Do something*/}
};
then the compiler will accept it, and allow this to happen.
i've tested this against GCC 8.1 and GCC 4.6, and both have the same behaviour. is this a bug, or is it something that should be allowed? does deriving it as a template class implicitly place the TemplateDerivedMember
class as a member of Base
? (it doesn't appear to as it accessible in the global namespace)
Yes, it is a bug, which apparently was fixed since GCC 11. Both Clang and MSVC always rejected the code as far as I can see. Demo: https://gcc.godbolt.org/z/9evfYq5hc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.