VS2015 Update 1 bug, or bad C++: Why can't a friend class access its friend's protected destructor?

末鹿安然 提交于 2019-12-04 01:48:47

I am not 100% sure on your exact problem, but it reminds me of a problem I had a while ago, where forward declared classes would have an unexpected scope. this page cppreference class highlights the rules, that a forward-declared class has the most local scope. However, your example on my VS2015u3 does not fail either.

I think the fix is probably to forward declare the class which is a friend before the class, so that it has a well defined scope.

When you have a class such as

class Example {
     int someFunction( class SomeOtherClass & param );
};

The compiler treats declaration of SomeOtherClass which is within the local scope.

This means that

class Example {
     int someFunction( class SomeOtherClass & param );
};

class SomeOtherClass {
          ...
};

Declares three classes Example Example::SomeOtherClass and SomeOtherClass

Changing your example to

class Foo_init;

class Foo {
  protected:
    virtual ~Foo() {}

    friend Foo_init;
 };

class Foo_init {
  public:
    Foo init;
 };

 static Foo_init staticFooInit;

Should work

You have used an identifier that starts with an underscore and then a capital letter. These names are reserved for the implementation, and using them in user code is undefined behaviour.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!