Click to See Complete Forum and Search --> : constructor member initialization lists


Witching Hour
01-06-2001, 07:13 AM
in the following example, i created a class to set the winapi WNDCLASS
structure and register that class, but this class can't work without
arguments being passed to the overloaded constructor, in this example they
are wndclass, hinstance, address of wndproc function and the name of that
wndclass structure.

i have also created a default constructor and placed it in private, so it
won't be accessed, but i cannot write the implementation for that default
constructor because hinst and wndproc are references and they are required
in the member initialization list, and i can't set them to NULL.
leaving the default constructor without implementation works, and not
declaring a default constructor also worked, but i want to declare it and
write the implementation :) so plz help, this is the first time that i used
c++ with winapi

thank you

here's the code:

///////////////////////////////////////////////////////////////////
class InitClass
{
public:
InitClass(WNDCLASS *wndclass,HINSTANCE & hInst,WNDPROC wp,TCHAR
*classname);
~InitClass();
bool RegisterInitClass();
private:
InitClass(); // what's the implementation for this default constructor

WNDCLASS *initclasswc;
HINSTANCE &initclasshInst; // must be in member initialization list
WNDPROC &initclasswndproc;
TCHAR *initclassname;
};
//////////////////////////////////////////////////////////
//implementation for the InitClass class
/////////////////////////////////////////////////////////

//destructor

InitClass::~InitClass()
{
}

//overloaded constructor

InitClass::InitClass(WNDCLASS *wndclass,HINSTANCE &hInst,WNDPROC wp,TCHAR
*classname)
:initclasswc(wndclass),initclasshInst(hInst),initclasswndproc(wp),
initclassname(classname)
{
initclasswc->style = CS_HREDRAW | CS_VREDRAW;
initclasswc->lpfnWndProc = initclasswndproc;
initclasswc->cbClsExtra = 0;
initclasswc->cbWndExtra = 0;
initclasswc->hInstance = initclasshInst;
initclasswc->hIcon = LoadIcon(NULL,IDI_APPLICATION);
initclasswc->hCursor = LoadCursor(NULL,IDC_ARROW);
initclasswc->hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
initclasswc->lpszMenuName = NULL;
initclasswc->lpszClassName = initclassname;
}

bool InitClass::RegisterInitClass()
{
if(!::RegisterClass(initclasswc))
{
::MessageBox(NULL,"Registration Failed!!","Error",0);
return false;
}
return true;
}

Danny Kalev
01-06-2001, 07:43 AM
you can't really define a default ctor in this case; the common approach
is to declare a private default ctor and leave it unimplemeneted. This
way, users can't call it accidentally (they will get a compilation
error) and in addition, you won't have to implement it.

Danny

Witching Hour wrote:
>
> in the following example, i created a class to set the winapi WNDCLASS
> structure and register that class, but this class can't work without
> arguments being passed to the overloaded constructor, in this example they
> are wndclass, hinstance, address of wndproc function and the name of that
> wndclass structure.
>
> i have also created a default constructor and placed it in private, so it
> won't be accessed, but i cannot write the implementation for that default
> constructor because hinst and wndproc are references and they are required
> in the member initialization list, and i can't set them to NULL.
> leaving the default constructor without implementation works, and not
> declaring a default constructor also worked, but i want to declare it and
> write the implementation :) so plz help, this is the first time that i used
> c++ with winapi
>
> thank you
>
> here's the code:
>
> ///////////////////////////////////////////////////////////////////
> class InitClass
> {
> public:
> InitClass(WNDCLASS *wndclass,HINSTANCE & hInst,WNDPROC wp,TCHAR
> *classname);
> ~InitClass();
> bool RegisterInitClass();
> private:
> InitClass(); // what's the implementation for this default constructor
>
> WNDCLASS *initclasswc;
> HINSTANCE &initclasshInst; // must be in member initialization list
> WNDPROC &initclasswndproc;
> TCHAR *initclassname;
> };
> //////////////////////////////////////////////////////////
> //implementation for the InitClass class
> /////////////////////////////////////////////////////////
>
> //destructor
>
> InitClass::~InitClass()
> {
> }
>
> //overloaded constructor
>
> InitClass::InitClass(WNDCLASS *wndclass,HINSTANCE &hInst,WNDPROC wp,TCHAR
> *classname)
> :initclasswc(wndclass),initclasshInst(hInst),initclasswndproc(wp),
> initclassname(classname)
> {
> initclasswc->style = CS_HREDRAW | CS_VREDRAW;
> initclasswc->lpfnWndProc = initclasswndproc;
> initclasswc->cbClsExtra = 0;
> initclasswc->cbWndExtra = 0;
> initclasswc->hInstance = initclasshInst;
> initclasswc->hIcon = LoadIcon(NULL,IDI_APPLICATION);
> initclasswc->hCursor = LoadCursor(NULL,IDC_ARROW);
> initclasswc->hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
> initclasswc->lpszMenuName = NULL;
> initclasswc->lpszClassName = initclassname;
> }
>
> bool InitClass::RegisterInitClass()
> {
> if(!::RegisterClass(initclasswc))
> {
> ::MessageBox(NULL,"Registration Failed!!","Error",0);
> return false;
> }
> return true;
> }

Witching Hour
01-06-2001, 01:40 PM
yes thanx danny that's exactly what i did, did u see my code it's a bit far
down, so there's no way for sure to do it right?

"Danny Kalev" <dannykk@inter.net.il> wrote in message
news:3A57130D.84CF7082@inter.net.il...
> you can't really define a default ctor in this case; the common approach
> is to declare a private default ctor and leave it unimplemeneted. This
> way, users can't call it accidentally (they will get a compilation
> error) and in addition, you won't have to implement it.
>
> Danny
>
> Witching Hour wrote:
> >
> > in the following example, i created a class to set the winapi WNDCLASS
> > structure and register that class, but this class can't work without
> > arguments being passed to the overloaded constructor, in this example
they
> > are wndclass, hinstance, address of wndproc function and the name of
that
> > wndclass structure.
> >
> > i have also created a default constructor and placed it in private, so
it
> > won't be accessed, but i cannot write the implementation for that
default
> > constructor because hinst and wndproc are references and they are
required
> > in the member initialization list, and i can't set them to NULL.
> > leaving the default constructor without implementation works, and not
> > declaring a default constructor also worked, but i want to declare it
and
> > write the implementation :) so plz help, this is the first time that i
used
> > c++ with winapi
> >
> > thank you
> >
> > here's the code:
> >
> > ///////////////////////////////////////////////////////////////////
> > class InitClass
> > {
> > public:
> > InitClass(WNDCLASS *wndclass,HINSTANCE & hInst,WNDPROC wp,TCHAR
> > *classname);
> > ~InitClass();
> > bool RegisterInitClass();
> > private:
> > InitClass(); // what's the implementation for this default
constructor
> >
> > WNDCLASS *initclasswc;
> > HINSTANCE &initclasshInst; // must be in member initialization list
> > WNDPROC &initclasswndproc;
> > TCHAR *initclassname;
> > };
> > //////////////////////////////////////////////////////////
> > //implementation for the InitClass class
> > /////////////////////////////////////////////////////////
> >
> > //destructor
> >
> > InitClass::~InitClass()
> > {
> > }
> >
> > //overloaded constructor
> >
> > InitClass::InitClass(WNDCLASS *wndclass,HINSTANCE &hInst,WNDPROC
wp,TCHAR
> > *classname)
> > :initclasswc(wndclass),initclasshInst(hInst),initclasswndproc(wp),
> > initclassname(classname)
> > {
> > initclasswc->style = CS_HREDRAW | CS_VREDRAW;
> > initclasswc->lpfnWndProc = initclasswndproc;
> > initclasswc->cbClsExtra = 0;
> > initclasswc->cbWndExtra = 0;
> > initclasswc->hInstance = initclasshInst;
> > initclasswc->hIcon = LoadIcon(NULL,IDI_APPLICATION);
> > initclasswc->hCursor = LoadCursor(NULL,IDC_ARROW);
> > initclasswc->hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
> > initclasswc->lpszMenuName = NULL;
> > initclasswc->lpszClassName = initclassname;
> > }
> >
> > bool InitClass::RegisterInitClass()
> > {
> > if(!::RegisterClass(initclasswc))
> > {
> > ::MessageBox(NULL,"Registration Failed!!","Error",0);
> > return false;
> > }
> > return true;
> > }

Danny Kalev
01-06-2001, 03:45 PM
I think your code is fine. However, if your class is going to serve as a
base for other derived classes, you should declare the dtor virtual.

Danny

Witching Hour
01-06-2001, 05:17 PM
k thanx danny :)
"Danny Kalev" <dannykk@inter.net.il> wrote in message
news:3A5783F3.3492B987@inter.net.il...
> I think your code is fine. However, if your class is going to serve as a
> base for other derived classes, you should declare the dtor virtual.
>
> Danny