Greg Ercolano wrote:
> What you don't want to have is this:
>
> {
> char s[80]; sprintf(s, "Frame %04d", 1234);
> widget.label(s); // BAD! (s goes out of scope
> before widget does)
> }
>
> ..or this:
>
> {
> static char s[80];
> sprintf(s, "Frame %04d", 1234); widget1.label(s); // OK:
> s is static
> sprintf(s, "Frame %04d", 4567); widget2.label(s); // BAD!
> Reusing s[] will cause widget1 and widget2 to have same value
> }
Oh, and I forgot to mention; in those above cases where using label() is bad,
using copy_label() would work, because then the widget makes a copy of the
string
that is managed by the widget internally.
Why not always use copy_label()? You can, and it will be safe,
it's just that this makes copies in all cases, even situations where
a copy isn't needed.
For instance, widget.copy_label("Test"); makes an 'unnecessary copy'
because "Test" is a static string that is part of your binary, and
will remain allocated, but copy_label() will make another copy of it
in dynamic memory. Just an extra 5 bytes you might say, but in a large
program it can add up.
Typically widgets are part of classes, and classes can often keep
track of the data more efficiently. So a good use of label() with
dynamic strings would be:
class MyClass {
// Dynamic strings to hold name and address
std::string name;
std::string addr;
// Box widgets for name/address
Fl_Box *name_box;
Fl_Box *addr_box;
[..]
MyClass::LoadData(char *filename) {
[[..load strings from file..]]
name_box->label(name.c_str()); // OK: 'name' string
lasts as long as box
addr_box->label(address.c_str()); // OK: 'addr' string
lasts as long as box
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk