473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nested class and architecture of C++ class

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.

Oct 13 '08 #1
2 2564
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
Oct 13 '08 #2
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.
Oct 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Oschler | last post by:
Hello, I am a Python newbie (by experience, not chronologically :) ), so if any of this doesn't make sense my apologies in advance. I am reading the chapter in The Python Cookbook on databases...
5
by: Dave Benjamin | last post by:
I ran into an odd little edge case while experimenting with functions that create classes on the fly (don't ask me why): >>> def f(x): ... class C(object): ... x = x ... print C.x ......
17
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
5
by: Stephane Routelous | last post by:
Hi, I would like to make a forward declaration of a strcuture nested in a class. I have file A.h class A { public: struct B
4
by: Tadeusz S. | last post by:
Hello In Pascal, I use constructions like that var SomeVar = object SomeVar2 = object end; end; etc., so I could put my object into another
10
by: msnews.microsoft.com | last post by:
Public Class Controller Dim mx As New HelperClass 'here, where I have it now ???? Sub New() 'or here??? Dim mx As New HelperClass
9
by: DBC User | last post by:
Hi All, Would like to know which is the best approach. I have a class which has only constants variables (private) and a public static method, which returns a string variable. I would like to...
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
4
by: Ares Lagae | last post by:
How is an output stream operator of a class nested in a class template defined? The code fragment below does not compile. Maybe it's just me, but I don't see why it should not compile. Best...
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.