簡體   English   中英

VS2015更新1錯誤,或錯誤的C ++:為什么朋友類不能訪問其朋友的受保護的析構函數?

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

以下似乎是ZeroC ICE在其自動生成的代碼中使用的模式,在我看來,這是他們現在為他們的工具的許多版本制作單例(不確定原因)的一種方式。 各種編譯器都沒有問題,直到我今天發現Visual Studio 2015 Update 1(VS版本14.0.24720.00,VC ++版本19.00.23506)發出錯誤。 在更新1之前,VS2015也沒有問題。 我不確定這是一個帶有Update 1的VS2015 C ++編譯器中的錯誤(回歸?),還是其他編譯器允許滑動的錯誤(不符合標准的)C ++代碼。

以下是代碼模式的示例:

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

    friend class Foo_init;
};

class Foo_init {
public:
    Foo init;
};

static Foo_init staticFooInit;

VS2015 Update 1會發出以下錯誤:

example.cpp(13): error C2248: 'Foo::~Foo': cannot access protected member declared in class 'Foo'
example.cpp(3): note: see declaration of 'Foo::~Foo'
example.cpp(1): note: see declaration of 'Foo'

我發現了一個(尚未回答) ZeroC ICE論壇帖子似乎與此相關,但我沒有在我的Google搜索中發現任何讓我信服這是否是編譯器問題或錯誤代碼的信息。 我承認我不太了解ZeroC ICE,也沒有足夠的C ++朋友課程來深入了解你能做什么和不能做什么。 我希望有更多知識淵博的人可以對此有所了解。

我對你確切的問題並不是100%肯定,但它讓我想起了我前一段時間遇到的一個問題,前向聲明的類會有一個意想不到的范圍。 此頁面cppreference類突出顯示規則,前向聲明的類具有最本地范圍。 但是,我的VS2015u3上的示例也沒有失敗。

我認為修復可能是在類之前聲明類是朋友的類,因此它具有明確定義的范圍。

當你有一個類如

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

編譯器處理在本地范圍內的SomeOtherClass聲明。

這意味着

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

class SomeOtherClass {
          ...
};

聲明三個類Example Example::SomeOtherClassSomeOtherClass

將您的示例更改為

class Foo_init;

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

    friend Foo_init;
 };

class Foo_init {
  public:
    Foo init;
 };

 static Foo_init staticFooInit;

應該管用

您使用的標識符以下划線開頭,然后是大寫字母。 這些名稱是為實現保留的,在用戶代碼中使用它們是未定義的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM