Có thể ai đó giải thích cho tôi tại sao mã này:Weird lỗi biên dịch và mẫu thừa kế
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() {}
};
Tạo lỗi biên dịch sau đây?
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'
Kể từ khi cả hai safe_bool
mẫu lấy từ safe_bool_base
, tôi không hiểu tại sao người ta không thể truy cập vào một thành viên bảo vệ của lớp cơ sở.
Tôi có thiếu gì đó không?
Đây là một câu hỏi hay. Đề xuất bạn thêm thẻ 'được bảo vệ', 'cơ sở', 'có nguồn gốc' cũng như câu hỏi cho điều này để tìm kiếm/tham khảo – Chubsdad
@Chubsdad: Cảm ơn. Tôi chỉ có thể thêm một thẻ nữa. (5 là số lượng thẻ tối đa được phép.) – ereOn