簡體   English   中英

使用派生類的朋友提供的基類的受保護靜態函數

[英]Use protected static function of the base class from the friend of the derived class

不同的編譯器對這個問題似乎有不同的看法。 以下代碼使用gcc正常gcc ,但是使用clang失敗:

class Base {
protected:
    static void f() {}
};

class Derived : public Base {
    friend class DerivedFriend;
};

class DerivedFriend {
public:
    void g() {
        Base::f();
    }
};

clang的錯誤是:

main.cpp:13:15: error: 'f' is a protected member of 'Base'
        Base::f();
              ^
main.cpp:3:17: note: declared protected here
    static void f() {}
                ^
1 error generated.

這是CWG問題1873 ,它更改了此情況的規則([class.access.base] / p5):

如果在類N命名,則成員m在點R處可訪問。

  • [...]
  • 作為N的成員的m受保護,並且R出現在N類的成員或朋友中,或發生在從N派生的P類的成員或朋友中,其中m作為P的成員是公共的,私有的或受保護的,要么
  • [...]

在這里, NBasePDerivedmf() ,並且R出現在DerivedFriend的成員中; 在CWG1873之前,這是允許的,但是CWG1873刪除了“派生類的朋友”案例,並使該案例格式不正確。

解決方法是將f稱為Derived的成員,而不是Base

暫無
暫無

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

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