Perth ADUG Meeting (Online)

Woah. I completely forgot to add here (!) :persevere:

Zoom meeting.

Topic: ADUG Perth Meeting - Regroup, Talk, and Planning

Thursday 8th February 2024

6:00pm Meeting open (Perth) … 9pm Eastern time (DST)
6:15pm Presentations start

Presenter - Scott Hollows

Topic - Grab bag of Delphi tips and techniques

Tips and reviews of Delphi code features and quirks based on Scott’s work in Delphi over the past year. It would be nice to have some kind of theme going here, but the closest thing to that is “there is no moral to this story, its just a bunch of stuff that happened” (quote from the Simpsons, lawyer explaining their court defence to a judge) - so random interesting Delphi stuff

As a fun side topic, Scott has been involved in the Technology Museum of WA and will provide an update on what the museum is doing and how you can get involved.

Scott has been involved in the Delphi world since its predecessor Turbo Pascal developing a wide range of applications and helping out on the WA and national committees of ADUG.

[Editor Stan Lee here :
Wow, that is quite the understatement, Buckaroo. Ok, I’m out, true believers. Excelsior! ]

Join Zoom Meeting

Meeting ID: 829 5785 3513
Passcode: 400123

Also you can find us on Meetup dot com now.

During our February meeting, when @scott.hollows was making a point about const [ref] with regard to inheritance and function parameters, I made a mildly successful attempt to explain how C++ can deal with these issues.

(Delphi info eg : delphi - Can I force `const` to pass by reference (aka the missing `in` parameter) - Stack Overflow )

I thought I could do a better job in a few minutes, and just record a better explanation
… instead I have a long, slow battle with my cranial archive to remember stuff I have figured out in the past.

The end result is much longer than I planned, and of limited interest … but for what its worth, here it is :

Or … just see the final code :

#include <iostream>
 C++ has high granularity of its specification of constness around values and pointers

 - value
 - constant value

 - pointer to a value
 - pointer to a constant value

 - constant pointer to a (changeable) value
 - constant pointer to a constant value


int i = 1;
int j = 2;

const int k = 42;

int* pi = &i;             // a pointer to int, can be changed

const int * cpi = &i;     // the pointer CAN    be changed
						  // the TARGET  CANNOT be changed

int* const kpi = &i;      // the pointer CANNOT be changed
						  // the TARGET  CAN    be changed

const int* const xi = 0;  // the pointer CANNOT be changed
						  // the TARGET  CANNOT be changed

int main()
   i = 5;                 //     {normal integer can be changed}
   i = 10;

   //k = 1;  error               {error - const int cannot be changed}

   std::cout << "\n";
   std::cout << "1. Plain pointer pi can be changed, as expected \n";
   std::cout << "\n   " << pi << "\n";
   pi = &j;
   std::cout << "\n   " << pi << "\n\n";

   pi = &i;
   std::cout << "3. Plain pointer pi 's target can be changed, as expected \n";
   std::cout << "\n   " << *pi << "\n";
   *pi = 11;
   std::cout << "\n   " << *pi << "\n\n";

   std::cout << "4. (Const int)* pointer = 'Pointer to a Constant Integer' \n\n";
   std::cout << "   (Const int)* pointer cpi 's pointer can    be changed ... OK \n";
   std::cout << "   (Const int)* pointer cpi 's target  cannot be changed ... OK \n";
   cpi = &i;
   std::cout << "\n   " << *cpi << "\n";
   //*cpi = 100;                 {error - const int* target cannot be changed}
   std::cout << "\n   " << *cpi << "\n\n";

   std::cout << "5. (int)* const pointer = 'Constant Pointer to a (mutable) Integer' \n\n";
   std::cout << "   (int)* const pointer kpi 's pointer cannot be changed ... OK \n";
   std::cout << "   (int)* const pointer kpi 's target  can    be changed ... OK \n";
   std::cout << "\n   " << *kpi << "\n";
   //kpi = &i;                   {error - int* const pointer cannot be reassigned}
   *kpi = 100;
   std::cout << "\n   " << *kpi << "\n\n";

   std::cout << "6. (Const int)* const pointer = 'Constant Pointer to a Constant Integer' \n\n";

   Notes :

   1. If I knew what I was doing before I started,
	  I should have called  'cpi'  as  'pci', ie 'pointer to constant integer'.

   2. While a 'pointer to const' can give some protection of an integer value,
	  If the target is a normal, mutable integer ...
	  then the target can be changed by other parts of the program.

   3. Only a 'pointer to const' can be assigned the address of a constant integer.
	  It can be assigned to a normal integer, and no changes can be effected via that pointer.

   4. The whole "East Const" vs "West Const" rivalry is based on the nutbag idea that, assuming reading types from right to left is what real programmers do, then ``` int const * ``` and ``` int * const ``` read slightly more sanely that wsy. But bear in mind, it's the sanity of C+× programmers that we are talking here,  so ... 🤨 😅


Output :