[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Nested class and architecture of C++ class

John Doe

10/13/2008 9:55:00 AM

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.



2 Answers

Bo Persson

10/13/2008 6:08:00 PM

0

John Doe wrote:
> 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


asm23

10/14/2008 12:42:00 AM

0

John Doe wrote:
> 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.