C++ dot operators how to convert to arrow operators
Mar 13, 2019 16:55:01 GMT 1
Post by bigbass on Mar 13, 2019 16:55:01 GMT 1
Hello Guys
I am going to take a step to document in my own words
something that can be rather complex
to sort out if you are not used to
thinking in C++ this question has come up several times
and will come up again I'm sure
The first step to port C++ code to BaCon (this is new land to discover)
is to understand what you are reading first
with some working simple demo example as a reference
now that we can port GUI toolkits to BaCon
with an end goal to simplify the sometimes cryptic looking syntax
we will find code syntax written in different ways
one common syntax is the dot operator ( something.member)
another way is the arrow operator (something->member)
if this is not clear reading/porting code will be a big problem
usually turning off people before they ever get started passing this awkward hurdle
I will use erco's cheetsheet as a reference
so that I can just focus on the conversion process
of the dot and arrow operators
seriss.com/people/erco/fltk/#Fl_File_Chooser
Now I will rewrite it as arrow operator code below
look at the comments I added in the code they will explain whats going on
in the future posts I will show the porting process to BaCon's syntax
that allows you to compile with the bacon compiler
Joe
I am going to take a step to document in my own words
something that can be rather complex
to sort out if you are not used to
thinking in C++ this question has come up several times
and will come up again I'm sure
The first step to port C++ code to BaCon (this is new land to discover)
is to understand what you are reading first
with some working simple demo example as a reference
now that we can port GUI toolkits to BaCon
with an end goal to simplify the sometimes cryptic looking syntax
we will find code syntax written in different ways
one common syntax is the dot operator ( something.member)
another way is the arrow operator (something->member)
if this is not clear reading/porting code will be a big problem
usually turning off people before they ever get started passing this awkward hurdle
I will use erco's cheetsheet as a reference
so that I can just focus on the conversion process
of the dot and arrow operators
seriss.com/people/erco/fltk/#Fl_File_Chooser
Now I will rewrite it as arrow operator code below
look at the comments I added in the code they will explain whats going on
in the future posts I will show the porting process to BaCon's syntax
that allows you to compile with the bacon compiler
Joe
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_File_Chooser.H>
//
// Demonstrate how to use Fl_File_Chooser
// erco 08/04/2005
//http://seriss.com/people/erco/fltk/#Fl_File_Chooser
// modified to use and demonstrate "->" notation bigbass 03/13/2019
// I am going to use one of erco's cheetsheet demo examples
// as a reference base then I will convert it to use the arrow operator "->"
// how you can see and understand two different ways to code
// the dot "." and the arrow "->"
// for easy things the dot works with less syntax
// but... for most code in C++ larger apps you need to maintain
// a clean way to free memory used and for this reason
// the (something = new Fl_File_Chooser) is stored in memory
// by the use of the (new) command then cast to a widget type as a pointer
// we get a "simple easy way" to handle memory management on the low level
// compared to using standard C code thinking with malloc
// Callback: when use picks 'File | Open' from main menu
void open_cb(Fl_Widget*, void*) {
// Create the file chooser, and show it
Fl_File_Chooser* chooser = new Fl_File_Chooser(".", // directory
"*", // filter
Fl_File_Chooser::MULTI, // chooser type
"Title Of Chooser"); // title
chooser->show();
// Block until user picks something.
// (The other way to do this is to use a callback())
//
while(chooser->shown())
{ Fl::wait(); }
// User hit cancel?
if ( chooser->value() == NULL )
{ fprintf(stderr, "(User hit 'Cancel')\n"); return; }
// Print what the user picked
fprintf(stderr, "--------------------\n");
fprintf(stderr, "DIRECTORY: '%s'\n", chooser->directory());
fprintf(stderr, " VALUE: '%s'\n", chooser->value());
fprintf(stderr, " COUNT: %d files selected\n", chooser->count());
// Multiple files? Show all of them
if ( chooser->count() > 1 ) {
for ( int t=1; t<=chooser->count(); t++ ) {
fprintf(stderr, " VALUE[%d]: '%s'\n", t, chooser->value(t));
}
}
}
// Callback: when user picks 'Quit'
void quit_cb(Fl_Widget*, void*) {
exit(0);
}
int main() {
Fl_Window* win = new Fl_Window(300, 180, "Simple Example of Fl_File_Chooser");
Fl_Menu_Bar* menubar = new Fl_Menu_Bar(0,0,300,25);
menubar->add("File/Open", 0, open_cb);
menubar->add("File/Quit", 0, quit_cb);
win->show();
return(Fl::run());
}