有人能解釋我爲什麼這個代碼:奇怪的編譯器錯誤和模板繼承
class safe_bool_base
{ //13
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {} //18
safe_bool_base() {}
safe_bool_base(const safe_bool_base&) {}
safe_bool_base& operator=(const safe_bool_base&) { return *this; }
~safe_bool_base() {}
};
template <typename T=void> class safe_bool : public safe_bool_base
{
public:
operator bool_type() const
{
return (static_cast<const T*>(this))->boolean_test() ? &safe_bool_base::this_type_does_not_support_comparisons : 0;
}
protected:
~safe_bool() {}
};
template <> class safe_bool<void> : public safe_bool_base
{
public:
operator bool_type() const
{
return (boolean_test() == true) ? &safe_bool_base::this_type_does_not_support_comparisons : 0; //46
}
protected:
virtual bool boolean_test() const = 0;
virtual ~safe_bool() {}
};
產生如下的編譯器錯誤?
c:\project\include\safe_bool.hpp(46) : error C2248: 'safe_bool_base::this_type_does_not_support_comparisons' : cannot access protected member declared in class 'safe_bool_base'
c:\project\include\safe_bool.hpp(18) : see declaration of 'safe_bool_base::this_type_does_not_support_comparisons'
c:\project\include\safe_bool.hpp(13) : see declaration of 'safe_bool_base'
由於兩個safe_bool
模板從safe_bool_base
派生,我不明白爲什麼人們不能訪問基類的保護成員。
我錯過了什麼嗎?
這是一個很好的問題。建議您添加標記'受保護','基','派生'以及這個問題在搜索/參考 – Chubsdad 2010-09-07 10:29:13
@Chubsdad:謝謝。我只能添加一個標籤。 (5是最大標籤數afaik。) – ereOn 2010-09-07 11:35:23