Notifications
Clear all

C++ Tips - Understanding "scope"

16 Posts
2 Users
11 Likes
1,599 Views
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 

@frogandtoad Excellent tutorial. I never used C++ it wasn't available on my hardware but I did study it a bit and obviously either don't remember very much or never got to that part.

I do like how the resolution is so specific by using the :: symbol, I didn't know that was called a scope resolution operator, like I said it's been a while (almost 40 years!).

Pardon my ignorance but is namespace a keyword or just an example?

Sorry, been a document critic for a long time and my eyes pick up on these things automatically, but you have a typo,

delvelop

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @zander

@frogandtoad Excellent tutorial. I never used C++ it wasn't available on my hardware but I did study it a bit and obviously either don't remember very much or never got to that part.

I do like how the resolution is so specific by using the :: symbol, I didn't know that was called a scope resolution operator, like I said it's been a while (almost 40 years!).

Pardon my ignorance but is namespace a keyword or just an example?

Sorry, been a document critic for a long time and my eyes pick up on these things automatically, but you have a typo,

delvelop

Thanks, and yes, 'namespace' is an ISO C++ keyword - There are many things that the Arduino language reference does not talk about, but still very applicable within the language, especially as I mentioned for avoiding library conflicts.

[edit] - Thanks for picking up on the type ... fixing it now 🙂

Cheers


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 

@frogandtoad Will you be doing more of these tips to point out items like this especially if the Arduino language reference does not touch on them or doesn't cover them adequately?

Sorry to use your thread for one of my personal soap boxes, but this post is another example of where a 'files' section like most forums have would be the place to save the original tips/tutorials without the forum conversation. I was trying to follow a similar tutorial and had to give up on it as it is now mostly comments and simply too time consuming to discover the meat. As a seniour member I am hoping you can see the merit of my idea and support a feature request for a fies section.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @zander

@frogandtoad Will you be doing more of these tips to point out items like this especially if the Arduino language reference does not touch on them or doesn't cover them adequately?

Sorry to use your thread for one of my personal soap boxes, but this post is another example of where a 'files' section like most forums have would be the place to save the original tips/tutorials without the forum conversation. I was trying to follow a similar tutorial and had to give up on it as it is now mostly comments and simply too time consuming to discover the meat. As a seniour member I am hoping you can see the merit of my idea and support a feature request for a fies section.

Sure, I'm more than happy to highlight some more of the hidden C++ powers (that I am aware of) available.  As I understand it, there are a few more features available for the basic UNO, but other microprocessors such as ESP32 offer even more support for the ISO C++ library, which I will have to test before confirming in future.

FYI: - The features I will be showing for the UNO are from C++11 (though the whole C++11 functionality is not supported).  Other microprocessors are thought to support C++14 - C++20 functionality with some compiler switches... something I need to test in future.

Cheers


   
Inst-Tech reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 

@frogandtoad Sounds good.

I see you mentioned 'compiler switches'. Do you know how to provide them when using the IDE and where is each one documented. As a former troubleshooter, I often was able to resolve problems with just a compiler switch, in one case a very seniour competent customer had been trying for 2 years to find an issue that I found in a couple minutes by adding a compiler switch.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @zander

@frogandtoad Sounds good.

I see you mentioned 'compiler switches'. Do you know how to provide them when using the IDE and where is each one documented. As a former troubleshooter, I often was able to resolve problems with just a compiler switch, in one case a very seniour competent customer had been trying for 2 years to find an issue that I found in a couple minutes by adding a compiler switch.

Sure - You can provide the compiler switches in your platform.txt file or platform.local.txt file if you created one.

Cheers


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 
Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad Sounds good.

I see you mentioned 'compiler switches'. Do you know how to provide them when using the IDE and where is each one documented. As a former troubleshooter, I often was able to resolve problems with just a compiler switch, in one case a very seniour competent customer had been trying for 2 years to find an issue that I found in a couple minutes by adding a compiler switch.

Sure - You can provide the compiler switches in your platform.txt file or platform.local.txt file if you created one.

Cheers

Ah, I forgot about that. I just learned of the platform.local.txt file a few days ago but I don't recall the context. I just had a quick look at one of the hardware platform.txt files. OMG, things have certainly got a lot more complicated since I was last messing with anything like that. I just googled 'gc switches' and found the gnu site. WOW, that's a LOT of information. Any experts about want to simplify them????

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad Excellent tutorial. I never used C++ it wasn't available on my hardware but I did study it a bit and obviously either don't remember very much or never got to that part.

I do like how the resolution is so specific by using the :: symbol, I didn't know that was called a scope resolution operator, like I said it's been a while (almost 40 years!).

Pardon my ignorance but is namespace a keyword or just an example?

Sorry, been a document critic for a long time and my eyes pick up on these things automatically, but you have a typo,

delvelop

Thanks, and yes, 'namespace' is an ISO C++ keyword - There are many things that the Arduino language reference does not talk about, but still very applicable within the language, especially as I mentioned for avoiding library conflicts.

[edit] - Thanks for picking up on the type ... fixing it now 🙂

Cheers

FYI: - Here is an example of using a namespace for your own library:

namespace myLibrary {
  struct Servo
   {
    int angle = 42;  // Assume we recieved it from a sensor

    int getAngle() {
      return angle; 
     }
   };
 }

// Servo Servo1; // Servo is inaccessible (will not compile) without scope qualification
myLibrary::Servo Servo1; // We need to quallify a namespace to access it to create our object

// We can also quallify what we want to use by using the 'using' clause (essentially an alias)
using myLibrary::Servo; 

// Thus (after our using qualification), we can now declare our object as 
// follows (because we brought it into scope):
Servo Servo2; void setup() {  Serial.begin(9600);  Serial.println(Servo1.getAngle());  Serial.println(Servo2.getAngle()); } void loop() {  // ... }

Hopefully, this demonstrates the power of namespaces, and helps people to understand how they work, and what to look for if they receive scope type errors.

Cheers.


   
Inst-Tech reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @zander
Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad Sounds good.

I see you mentioned 'compiler switches'. Do you know how to provide them when using the IDE and where is each one documented. As a former troubleshooter, I often was able to resolve problems with just a compiler switch, in one case a very seniour competent customer had been trying for 2 years to find an issue that I found in a couple minutes by adding a compiler switch.

Sure - You can provide the compiler switches in your platform.txt file or platform.local.txt file if you created one.

Cheers

Ah, I forgot about that. I just learned of the platform.local.txt file a few days ago but I don't recall the context. I just had a quick look at one of the hardware platform.txt files. OMG, things have certainly got a lot more complicated since I was last messing with anything like that. I just googled 'gc switches' and found the gnu site. WOW, that's a LOT of information. Any experts about want to simplify them????

🙂

There are way too many switches, and I don't know them all - We only use them as the need arises.  The only ones you really need to consider for most general cases, is the log level/warning/error flags, and language support flags, unless you're getting into really advanced compilation.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 

@frogandtoad I think I recently saw some code that had a statement like 'Using mything;'. I had never seen that before but I have seen similar syntax for something similar, maybe a struct and if you say using mystruct then you can leave off all the mystruct. bits. Am I remembering that right? Is that the same thing or just a similar thing. BTW, it was C where I think I encountered that, there was no C++ for our platform.

Hopefully, this demonstrates the power of namespaces, and helps people to understand how they work, and what to look for if they receive scope type errors.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @zander

@frogandtoad I think I recently saw some code that had a statement like 'Using mything;'. I had never seen that before but I have seen similar syntax for something similar, maybe a struct and if you say using mystruct then you can leave off all the mystruct. bits. Am I remembering that right? Is that the same thing or just a similar thing. BTW, it was C where I think I encountered that, there was no C++ for our platform.

Hopefully, this demonstrates the power of namespaces, and helps people to understand how they work, and what to look for if they receive scope type errors.

In modern C++ (actually, when I say modern, it's been around since C==98/9) 🙂

You may well have seen the following in a C++ program:

#include <string>
#include <vector>

using namespace std;

using std::string;
using std::vector;

int main()
 {
  // So rather than doing the following:
  std::string name = "foo";
  std::vector myList(10);

  // The using clause allows us to remove the namespace qualifier,
  // and now that we brought the symbols into scope, we can simply do:

  string name = "foo";
  vector myList(10);
 }

Cheers


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 
Posted by: @frogandtoad

@zander

Posted by: @zander
Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad Sounds good.

I see you mentioned 'compiler switches'. Do you know how to provide them when using the IDE and where is each one documented. As a former troubleshooter, I often was able to resolve problems with just a compiler switch, in one case a very seniour competent customer had been trying for 2 years to find an issue that I found in a couple minutes by adding a compiler switch.

Sure - You can provide the compiler switches in your platform.txt file or platform.local.txt file if you created one.

Cheers

Ah, I forgot about that. I just learned of the platform.local.txt file a few days ago but I don't recall the context. I just had a quick look at one of the hardware platform.txt files. OMG, things have certainly got a lot more complicated since I was last messing with anything like that. I just googled 'gc switches' and found the gnu site. WOW, that's a LOT of information. Any experts about want to simplify them????

🙂

There are way too many switches, and I don't know them all - We only use them as the need arises.  The only ones you really need to consider for most general cases, is the log level/warning/error flags, and language support flags, unless you're getting into really advanced compilation.

@frogandtoad I agree in general, but I have encountered a couple cases over the years of seemingly innocuous compiler switches actually doing much more than it would appear. In one case, a type of code mistake that was not flagged except at the very lowest limit (I perhaps, rc=2) that could result in a run time crash. In another case, a switch allowed the compiler to generate code that was 1/2 the execution time at the cost of fully specifying the run time JCL. I used to always run my compiles at the lowest/highest level (depends on your point of view) and fixed every one. Many were just in need of a cast, but I imagine you are aware that it is possible to not cast and get away with it but occasionally suffer a crash. It's been a very long time since I was that deep in the weeds and I suspect much has changed but I had a crash yesterday and I think I know why. I knew what I was doing was not something I was sure of but when it compied I assumed it was ok. After the crash I took that one line out and it was ok again.

I am only doing coding for fun now, nobody is paying me so I will probably not dive too deep into this but I will sniff around a bit.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
frogandtoad reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
Topic starter  

@zander

Posted by: @zander
Posted by: @frogandtoad

@zander

Posted by: @zander
Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad Sounds good.

I see you mentioned 'compiler switches'. Do you know how to provide them when using the IDE and where is each one documented. As a former troubleshooter, I often was able to resolve problems with just a compiler switch, in one case a very seniour competent customer had been trying for 2 years to find an issue that I found in a couple minutes by adding a compiler switch.

Sure - You can provide the compiler switches in your platform.txt file or platform.local.txt file if you created one.

Cheers

Ah, I forgot about that. I just learned of the platform.local.txt file a few days ago but I don't recall the context. I just had a quick look at one of the hardware platform.txt files. OMG, things have certainly got a lot more complicated since I was last messing with anything like that. I just googled 'gc switches' and found the gnu site. WOW, that's a LOT of information. Any experts about want to simplify them????

🙂

There are way too many switches, and I don't know them all - We only use them as the need arises.  The only ones you really need to consider for most general cases, is the log level/warning/error flags, and language support flags, unless you're getting into really advanced compilation.

@frogandtoad I agree in general, but I have encountered a couple cases over the years of seemingly innocuous compiler switches actually doing much more than it would appear. In one case, a type of code mistake that was not flagged except at the very lowest limit (I perhaps, rc=2) that could result in a run time crash. In another case, a switch allowed the compiler to generate code that was 1/2 the execution time at the cost of fully specifying the run time JCL. I used to always run my compiles at the lowest/highest level (depends on your point of view) and fixed every one. Many were just in need of a cast, but I imagine you are aware that it is possible to not cast and get away with it but occasionally suffer a crash. It's been a very long time since I was that deep in the weeds and I suspect much has changed but I had a crash yesterday and I think I know why. I knew what I was doing was not something I was sure of but when it compied I assumed it was ok. After the crash I took that one line out and it was ok again.

I am only doing coding for fun now, nobody is paying me so I will probably not dive too deep into this but I will sniff around a bit.

Exactly, just depends on your goals... in most cases, you won't need to go down to such levels of debugging and performance.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6930
 
Posted by: @frogandtoad

@zander

Posted by: @zander

@frogandtoad I think I recently saw some code that had a statement like 'Using mything;'. I had never seen that before but I have seen similar syntax for something similar, maybe a struct and if you say using mystruct then you can leave off all the mystruct. bits. Am I remembering that right? Is that the same thing or just a similar thing. BTW, it was C where I think I encountered that, there was no C++ for our platform.

Hopefully, this demonstrates the power of namespaces, and helps people to understand how they work, and what to look for if they receive scope type errors.

In modern C++ (actually, when I say modern, it's been around since C==98/9) 🙂

You may well have seen the following in a C++ program:

#include <string>
#include <vector>

using namespace std;

using std::string;
using std::vector;

int main()
 {
  // So rather than doing the following:
  std::string name = "foo";
  std::vector myList(10);

  // The using clause allows us to remove the namespace qualifier,
  // and now that we brought the symbols into scope, we can simply do:

  string name = "foo";
  vector myList(10);
 }

Cheers

@frogandtoad Since I never used C++ professionally, I am either thinking of C or maybe a different language like pascal where you could say something like 'with struct' or 'using struct' and then 'struct.member' just became 'member'. I was always a little ambivalent about using that, I wasn't sure if it helped or hurt the next person reading the code. Your examples are more complex and are actually needed in some cases to compile when libraries are used. I haven't encountered a conflict yet, but thanks for the forewarning.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 2