R2Delta
R2Delta

Reputation: 29

Access protected members in C++

How can I access protected members declared in class 'SecondDlg' within class 'ChooseDirDlg', if at all? Below are the class declarations:

class CChooseDirDlg : public CDialog
{
// Construction
public:
CChooseDirDlg(CWnd* pParent = NULL);    // standard constructor



class SecondDlg : public CDialog
{
// Construction
public:
SecondDlg(CWnd* pParent = NULL);   // standard constructor

Would changing the constructor to take a derived instance of one class solve the problem? And if so, how could I go about this?

Upvotes: 0

Views: 65

Answers (2)

Blacktempel
Blacktempel

Reputation: 3995

Inherit from it

class CChooseDirDlg : public SecondDlg
{}

Declare the class as friend

class SecondDlg : public CDialog
{
    friend class CChooseDirDlg;
}

Upvotes: 1

emvee
emvee

Reputation: 4449

Declare CChooseDirDlg as friend inside SecondDlg. This will grant CChooseDirDlg access to all members of SecondDlg, even the private ones.

I wouldn't have minded if C++ had implemented an acquaintance acces modifier to grant access to protected members ;-)

Upvotes: 1

Related Questions