Nested class and architecture of C++ class

J

John Doe

Hi,

I am trying to port some C class routines into C++.
THe original functions are use to set/get properties of buttons inside a
toolbar :

// This function is used to set btton text
// Parameters : dwIndexOrId : index or cmd id
// EGetMode : select button by ID or by cmd index

enum EGetMode
{
BY_CMD,
BY_INDEX
};

void SetButtonText(DWORD dwIndexOrId, EGetMode eGetMode)
{
TBBUTTONINFO tbbi;
DWORD dwFlags (eGetMode == BY_INDEX) ? TBIF_BYINDEX : 0;


tbbi.dwMask = TBIF_TEXT | dwFlags;
tbbi.pszText = (LPWSTR) a_szText;
tbbi.cchText = _tcsclen( a_szText );
return ::SendMessage (m_hCB, TB_SETBUTTONINFO, dwIndexOrId, (LPARAM)&tbbi);
}

Since using C++ I wanted to be able to write this :

CxCommandBarCe cmdBar(m_hWndCECommandBar);
cmdBar.GetButton(0, CxCommandBarCe::BY_CMD).SetWindowText(_T("foo"));


so I have coded the class below


So I started with the following class :

class CxCommandBarCe
{
public:
enum EGetMode
{
BY_CMD,
BY_INDEX
};


class CxButtonCe
{
public:
CxButtonCe()
{
ZeroMemory( &m_tbbi, sizeof(m_tbbi) );
m_tbbi.cbSize = sizeof( m_tbbi );
}

BOOL SetWindowText(LPCTSTR a_szText)
{
m_tbbi.dwMask = TBIF_TEXT | m_dwFlags;
m_tbbi.pszText = (LPWSTR) a_szText;
m_tbbi.cchText = _tcsclen( a_szText );
return ::SendMessage (m_hCB, TB_SETBUTTONINFO, m_dwIndexOrId,
(LPARAM)&m_tbbi);
}
void Get(HWND hWndCECommandBar, DWORD dwIndexOrId, EGetMode eGetMode)
{
m_hCB = hWndCECommandBar;
m_dwIndexOrId = dwIndexOrId;
m_dwFlags = (eGetMode == BY_INDEX) ? TBIF_BYINDEX : 0;
}

HWND m_hCB;
DWORD m_dwFlags;
DWORD m_dwIndexOrId;
TBBUTTONINFO m_tbbi;
};

CxCommandBarCe():
m_hCB(NULL),
m_nButtons(0)
{

}

CxCommandBarCe(HWND hWndCECommandBar)
{
m_hCB = hWndCECommandBar;

}

// Buttons
int GetButtonCount()
{
return ::SendMessage(m_hCB, TB_BUTTONCOUNT, 0, 0);
}

CxButtonCe& GetButton(DWORD dwIndexOrId, EGetMode eGetMode)
{
m_btnCe.Get(m_hCB, dwIndexOrId, eGetMode);
return m_btnCe;
}

protected:
HWND m_hCB;
int m_nButtons;
CxButtonCe m_btnCe;
};


I am not very satisified with it, for instance is there any means the
inner class access the m_hCB parent class ?
I find it stupid to duplicate it...
If GetButton fails I am returning a CxButton reference anyway, but how
caller can know it has failed ?

If you have some suggestions I am also all eyes.
 
B

Bo Persson

John said:
Hi,

I am trying to port some C class routines into C++.
THe original functions are use to set/get properties of buttons
inside a toolbar :

// This function is used to set btton text
// Parameters : dwIndexOrId : index or cmd id
// EGetMode : select button by ID or by cmd index

enum EGetMode
{
BY_CMD,
BY_INDEX
};

void SetButtonText(DWORD dwIndexOrId, EGetMode eGetMode)
{
TBBUTTONINFO tbbi;
DWORD dwFlags (eGetMode == BY_INDEX) ? TBIF_BYINDEX : 0;


tbbi.dwMask = TBIF_TEXT | dwFlags;
tbbi.pszText = (LPWSTR) a_szText;
tbbi.cchText = _tcsclen( a_szText );
return ::SendMessage (m_hCB, TB_SETBUTTONINFO, dwIndexOrId,
(LPARAM)&tbbi); }

Since using C++ I wanted to be able to write this :

CxCommandBarCe cmdBar(m_hWndCECommandBar);
cmdBar.GetButton(0,
CxCommandBarCe::BY_CMD).SetWindowText(_T("foo"));

so I have coded the class below


So I started with the following class :

class CxCommandBarCe
{
public:
enum EGetMode
{
BY_CMD,
BY_INDEX
};


class CxButtonCe
{
public:
CxButtonCe()
{
ZeroMemory( &m_tbbi, sizeof(m_tbbi) );
m_tbbi.cbSize = sizeof( m_tbbi );
}

BOOL SetWindowText(LPCTSTR a_szText)
{
m_tbbi.dwMask = TBIF_TEXT | m_dwFlags;
m_tbbi.pszText = (LPWSTR) a_szText;
m_tbbi.cchText = _tcsclen( a_szText );
return ::SendMessage (m_hCB, TB_SETBUTTONINFO, m_dwIndexOrId,
(LPARAM)&m_tbbi);
}
void Get(HWND hWndCECommandBar, DWORD dwIndexOrId, EGetMode
eGetMode) {
m_hCB = hWndCECommandBar;
m_dwIndexOrId = dwIndexOrId;
m_dwFlags = (eGetMode == BY_INDEX) ? TBIF_BYINDEX : 0;
}

HWND m_hCB;
DWORD m_dwFlags;
DWORD m_dwIndexOrId;
TBBUTTONINFO m_tbbi;
};

CxCommandBarCe():
m_hCB(NULL),
m_nButtons(0)
{

}

CxCommandBarCe(HWND hWndCECommandBar)
{
m_hCB = hWndCECommandBar;

}

// Buttons
int GetButtonCount()
{
return ::SendMessage(m_hCB, TB_BUTTONCOUNT, 0, 0);
}

CxButtonCe& GetButton(DWORD dwIndexOrId, EGetMode eGetMode)
{
m_btnCe.Get(m_hCB, dwIndexOrId, eGetMode);
return m_btnCe;
}

protected:
HWND m_hCB;
int m_nButtons;
CxButtonCe m_btnCe;
};


I am not very satisified with it, for instance is there any means
the inner class access the m_hCB parent class ?
I find it stupid to duplicate it...
If GetButton fails I am returning a CxButton reference anyway, but
how caller can know it has failed ?

If you have some suggestions I am also all eyes.

The inner class doesn't know which instance of the parent class it
should belong to, unless it is given a reference or a pointer to it.
Like passed to its constructor...


Bo Persson
 
A

asm23

John said:
Hi,

I am trying to port some C class routines into C++.
THe original functions are use to set/get properties of buttons inside a
toolbar :

// This function is used to set btton text
// Parameters : dwIndexOrId : index or cmd id
// EGetMode : select button by ID or by cmd index

enum EGetMode
{
BY_CMD,
BY_INDEX
};

void SetButtonText(DWORD dwIndexOrId, EGetMode eGetMode)
{
TBBUTTONINFO tbbi;
DWORD dwFlags (eGetMode == BY_INDEX) ? TBIF_BYINDEX : 0;


tbbi.dwMask = TBIF_TEXT | dwFlags;
tbbi.pszText = (LPWSTR) a_szText;
tbbi.cchText = _tcsclen( a_szText );
return ::SendMessage (m_hCB, TB_SETBUTTONINFO, dwIndexOrId, (LPARAM)&tbbi);
}

Since using C++ I wanted to be able to write this :

CxCommandBarCe cmdBar(m_hWndCECommandBar);
cmdBar.GetButton(0, CxCommandBarCe::BY_CMD).SetWindowText(_T("foo"));


so I have coded the class below


So I started with the following class :

class CxCommandBarCe
{
public:
enum EGetMode
{
BY_CMD,
BY_INDEX
};


class CxButtonCe
{
public:
CxButtonCe()
{
ZeroMemory( &m_tbbi, sizeof(m_tbbi) );
m_tbbi.cbSize = sizeof( m_tbbi );
}

BOOL SetWindowText(LPCTSTR a_szText)
{
m_tbbi.dwMask = TBIF_TEXT | m_dwFlags;
m_tbbi.pszText = (LPWSTR) a_szText;
m_tbbi.cchText = _tcsclen( a_szText );
return ::SendMessage (m_hCB, TB_SETBUTTONINFO,
m_dwIndexOrId, (LPARAM)&m_tbbi);
}
void Get(HWND hWndCECommandBar, DWORD dwIndexOrId, EGetMode
eGetMode)
{
m_hCB = hWndCECommandBar;
m_dwIndexOrId = dwIndexOrId;
m_dwFlags = (eGetMode == BY_INDEX) ? TBIF_BYINDEX : 0;
}

HWND m_hCB;
DWORD m_dwFlags;
DWORD m_dwIndexOrId;
TBBUTTONINFO m_tbbi;
};

CxCommandBarCe():
m_hCB(NULL),
m_nButtons(0)
{

}

CxCommandBarCe(HWND hWndCECommandBar)
{
m_hCB = hWndCECommandBar;

}

// Buttons
int GetButtonCount()
{
return ::SendMessage(m_hCB, TB_BUTTONCOUNT, 0, 0);
}

CxButtonCe& GetButton(DWORD dwIndexOrId, EGetMode eGetMode)
{
m_btnCe.Get(m_hCB, dwIndexOrId, eGetMode);
return m_btnCe;
}

protected:
HWND m_hCB;
int m_nButtons;
CxButtonCe m_btnCe;
};


I am not very satisified with it, for instance is there any means the
inner class access the m_hCB parent class ?
I find it stupid to duplicate it...
If GetButton fails I am returning a CxButton reference anyway, but how
caller can know it has failed ?

If you have some suggestions I am also all eyes.
I'm not quite understand all your code. But I do know that CxButtonCe is
a nested class in CxCommandBarCe.
If you want the inner class m_btnCe to access m_hCB? You can pass then
m_hCB as a parameter of CxButtonCe's constructor. then CxButtonCe can
operate on m_hCB internally. Another is to you can operate in
CxCommandBarCe 's constructor, because at this time, both the inner
instants were initialized.

You second question: IF you want to *return* more than one parameters,
you could just add a *reference* to the function's parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top