我将A类纳入C中包含的B女巫中。
在A班我有一个ExamItemStates函数。 我可以从B级访问它: ExamItemStates函数声明为Public:
BOOL ExamItemStates(int nItem, DWORD dwStates) const;
B级标题:
class B : public CDialogEx
{
DECLARE_DYNAMIC(B)
public:
B(CWnd* pParent = NULL); // standard constructor
enum { IDD = IDD_B };
CReportCtrl m_wndList;
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
};
//B.cpp
void B::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX, IDC_LIST1, m_wndList);
CDialogEx::DoDataExchange(pDX);
}
BOOL B::OnInitDialog()
{
CDialogEx::OnInitDialog();
if (m_wndList.ExamItemStates(2, RC_ITEM_CHECKED))
AfxMessageBox(L"Please write correct name!");
UpdateData(FALSE);
return TRUE;
}
我需要从C级访问它。我怎么能这样做?
答案 0 :(得分:0)
尝试使用protected
代替private
,方法一般应为public:
:
例如
class Foo
{
protected:
int a, b;
public:
void Method1();
};
提供您的头文件(带有类的文件)以获得更多解释。
这是可导出类的典型方案:
class Foo
{
private:
// elements that cannot be inherited (you cannot use them in the child classes) and cannot be accessed from outside of the class
protected:
// elements that can be inherited, but cannot be accessed from outside of the class
public:
// elements that can be inherited and accessed outside the class
};
示例代码:
class A
{
protected:
void DerivableMethod();
private:
int AccessibleOnlyInClassA;
};
class B : public A
{
protected:
void AnotherProtectedMethod();
private:
int AccessibleOnlyInClassB;
};
class C : public B
{
public:
void MethodInClasC()
{
DerivableMethod();
}
};