I have a class called Control. All other "controls" inherit from this class. Some controls need a default constructor so that I can assign or construct them later.
The classes look like:
class Control
{
private:
//All Members Here..
void Swap(Control &C);
public:
Control(DWORD dwExStyle, std::string Class, std::string Title, ....);
virtual ~Control();
virtual void Dispose();
virtual Control& operator = (Control C);
};
class Button : public Control
{
public:
Button(); //Default constructor.. I don't want to constructor a "Control" instance yet..
Button(std::string Title, Point Location, int Width, int Height, HWND Parent);
virtual ~Button();
virtual void Dispose() override;
virtual Button& operator = (Button B);
};
Control::~Control() {}
void Control::Swap(Control &C)
{
//Swap everything else.. and so on..
}
Control::Control(DWORD dwExStyle, std::string Class, std::string Title, ....) : //Initialize stuff..
{
Handle = CreateWindowEx(dwExStyle, Class.c_str(), Title.c_str(), ...); //creates a new Control.
}
void Control::Dispose()
{
ID = nullptr;
Parent = nullptr;
DestroyWindow(Handle);
}
Control& Control::operator = (Control C) //Basically Moves.. Steals all information and destructs old object.
{
if (this->Handle != C.Handle)
{
C.Swap(*this);
C.Dispose();
}
return *this;
}
Button::~Button() {}
//The next line is what I want to delay.. I am forced to call the parent constructor..
Button::Button() : Control(...) {}
Button::Button(std::string Title, Point Location, int Width, int Height, HWND Parent) : Create instance of Control.
Button& Button::operator = (Button B)
{
Control::operator = (B);
return *this;
}
Examples:
Button* Btn; //Create a button pointer.. Does not construct or create a window/button.
//WNDPROC......
WM_CREATE:
Btn = new Button("Title"...); //Constructs a button!
break;
I'm trying to do the above but on the stack instead.. See using a pointer, I'm able to delay the construction of the button later by calling new and constructing it there..
The below does the same thing via the stack except TWO objects are created.
Button Btn; //This constructs a button object.
//WNDProc....
WM_CREATE:
Btn = Button("Title"...); //Two buttons are now created.. The first is then destructed through assignment..
break;
I do not want to create two buttons.. However if I do:
WM_CREATE:
Button Btn = Button("Title"...); //Only one button is created but it is also destroyed when it goes out of scope.
break;
Is there a way I can delay the construction of the button without using a pointer like I did above? I cannot think of a good way to delay the construction of button. Control cannot have a default constructor and if it does I'd have to make it protected and determine which constructor was called so that I can later create the window.
Any ideas?