Re: Re: HOWTO = ???->show() in a callback function
Greg Ercolano
Sep 25, 2022
On 9/24/22 23:17, roger tunnicliffe wrote:
Thanks guys, I have found the difference but do not understand
it..
I had coded:-
Fl_Tabs *cards = new Fl_Tabs(40,40,300,200);
while your demonstration has coded:-
cards = new Fl_Tabs(40,40,300,200);
Would be very grateful if you could explain the
difference
You should probably
start by learning
the C++ language
first
using a tutorial
or book so you can
get down the basics
of the language.
Then segue into
FLTK once you're
comfortable with
C++.
With that said, the C++ programming topics you're
running into here are: "variable scope" -- once a
variable is declared, how long it remains visible "variable shadowing" -- how
variables of the same name can 'shadow' each other,
depending on scope "pointers"
-- accessing objects and memory via indirection
When you added the global variable
'cards', but also declared a local variable 'cards'
and initialized that, the global variable
remains unset, since the local variable shadows it.
This causes the global to remain
uninitialized.
And since the variable is a pointer, the machine
segfaults your app (crashes it) the moment
you try to indirect through the uninitialized
pointer (via cards->xxx), causing a memory
violation. You can read up on
C++ variable scoping rules here:
https://codescracker.com/cpp/cpp-scope-rules.htm
..and variable shadowing here:
https://www.learncpp.com/cpp-tutorial/variable-shadowing-name-hiding/ But this is kinda sliding off topic
for this forum, as these are questions not so much
about FLTK, and more about the basics of the C++ language
itself. I know it's hard to
know that when you're new to the language, which is why I
think it'll help if you learn
the language first.
Comments are owned by the poster. All other content is copyright 1998-2025 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.