|
|
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.
--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/fef6d8f3-f9ec-2532-b577-9244bba16293%40seriss.com.
[ Direct Link to Message ] | |
|
| |