idea4good / GuiLite

✔️The smallest header-only GUI library(4 KLOC) for all platforms
https://www.youtube.com/watch?v=grqXEz3bdC0
Apache License 2.0
7.42k stars 804 forks source link

Incorrect clone function #28

Closed Wanderson4096 closed 4 years ago

Wanderson4096 commented 5 years ago

There is several clone functions that actually does not clone the object. Like the one in button.h, it needs a *this parameter in the constructor.

idea4good commented 5 years ago

virtual c_wnd* clone(){return new c_button();} clone function will be used when you want clone a same UI, even it is rarely used. You refer to this code: clone 8 mini UI

Wanderson4096 commented 5 years ago

This clone function will not actually clone the object. You are creating a default object (using the default constructor) and not a copy. To actually build a copy of the object, you have to pass a pointer to this object (*this) as the constructor parameter, like this:

virtual c_wnd* clone(){return new c_button(*this);} // Makes a copy and returns a pointer to it.

idea4good commented 5 years ago

Maybe the name "clone" is not accurate. We just want a new c_wnd object, but do not want it's the same as its father.

idea4good commented 4 years ago

remove the function