I'm trying to create a Window class but for some reason a simple definition of a Window object is followed by calling it's destructor immediatly after.
The Window class' header has the following constructors and copy control defined:-
Window();
Window(int);
Window(const char* title);
Window(string title);
Window(const char* title, int x, int y, int width, int height);
Window(string title, int x, int y, int width, int height);
Window(const Window &window);
Window& operator=(const Window &window);
~Window();
The associated code for these functions is as follows:-
Window::Window()
{
Window(default_title, default_width, default_height, default_xpos, default_ypos);
}
Window::Window(int)
:title_(default_title),
size_({ default_width, default_height }),
position_({ default_xpos, default_ypos })
{
context_ = glutCreateWindow(title_.c_str());
setposition(position_);
setsize(size_);
glutSetWindow(context_);
}
Window::Window(string title)
:Window(title, default_width, default_height, default_xpos, default_ypos)
{ }
Window::Window(const char* title)
{
string t(title);
Window(t, default_width, default_height, default_xpos, default_ypos);
}
Window::Window(const char* title, int x, int y, int width, int height)
{
string t(title);
Window(t, width, height, x, y);
}
Window::Window(string title, int x, int y, int width, int height)
:title_(title),
size_({ width, height }),
position_({ x, y })
{
context_ = glutCreateWindow(title.c_str());
refresh();
setcallbacks();
glutSetWindow(context_);
}
Window::Window(const Window &window)
:title_(window.title_),
size_(window.size_),
position_(window.position_)
{
context_ = glutCreateWindow(title_.c_str());
refresh();
glutSetWindow(context_);
}
Window& Window::operator= (const Window &window)
{
title_ = window.title_;
size_ = window.size_;
position_ = window.position_;
context_ = window.context_;
refresh();
glutSetWindow(context_);
return *this;
}
Window::~Window()
{
glutDestroyWindow(context_);
}
None of the other functions used in the above code such as refresh() and setcallbacks() directly altar the class but instead call glut functions. If you think they're relevant I'll include them.
The problematic line is shown below, called as part of the main function:-
Window win("blah");
I've tried several configurations of this including the empty constructor, the full constructor and assignment but none seem to work. As far as I can tell, the constructor runs and initializes all the variables as expected then inexplicably calls the destructor when it advances to the next statement in the main function.