Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Variable Scope in C++

In C++, a variable is 'scoped' to the block in which it is declared. In other words, it exists from the point at which you tell the compiler what type of value it contains through to the close brace at that matches the open brace preceeding that declaration.

In the most frequent use, this means that a variable defined inside a loop doesn't exist after the loop has been exited. And that's very useful indeed in clearing down temporary memory storage for use within the loop's iterations. If you should happen to have another variable of the same name in the outer code (usually a daft thing to do!), its contents will NOT be visible within the inner block as they'll be masked by the more local variable, but they will be unmasked after the outer block is exited.

You can even set up a block that's not a loop and not a conditional - that's there purely to scope variables - if you wish. Simply use a pair of culry braces without any if, while or other keyword. Here's a code example that demsonstrates these features:

#include <iostream>
using namespace std;
 
int main () {
   int demo[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
   int k;
 
   int answer = 42;
 
   for (k=0; k<12; k++) {
 
      int answer = demo[k]; /* masks the original answer */
      cout << answer << ", ";
      }
 
   cout << endl << "hidden was " << answer << endl;
 
   { /* This block is only for scoping purposes */
   float answer = 3.1415;
   cout << answer << " is something to do with cherry pie" ;
   }
 
   cout << endl << "hidden was " << answer << endl; /* the original again */
}


When I run that code, my results look like this:

[trainee@easterton lp]$ ./vscpp
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
hidden was 42
3.1415 is something to do with cherry pie
hidden was 42
[trainee@easterton lp]$

(written 2009-01-22, updated 2009-01-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C231 - C and C based languages - Introduction to C++
  [4562] Left shift operator on an output stream object - C++ - (2015-10-30)
  [4561] Hello World in C++ - a first program, with the process explained - (2015-10-30)
  [4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
  [3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3053] Make - automating the commands for building and installing - (2010-11-16)
  [3052] Getting your C++ program to run - (2010-11-15)
  [2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
  [2169] When should I use OO techniques? - (2009-05-11)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [336] Targetted Advertising - (2005-06-05)
  [318] Choosing a theme - (2005-05-20)
  [317] Programming languages - a comparison - (2005-05-20)

C234 - C and C based languages - Further C++ Object Oriented features
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [3057] Lots of things to do with and within a C++ class - (2010-11-16)
  [2849] What are C++ references? Why use them? - (2010-07-02)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2576] What does const mean? C and C++ - (2010-01-15)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [1819] Calling base class constructors - (2008-10-03)
  [1159] It can take more that one plus one to get two. - (2007-04-22)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [802] undefined reference to typeinfo - C++ error message - (2006-07-15)
  [801] Simple polymorphism example - C++ - (2006-07-14)


Back to
Discount Training Courses - PHP, Perl, Python
Previous and next
or
Horse's mouth home
Forward to
Variables and pointers and references - C and C++
Some other Articles
The Month Ahead - What is happening in Melksham
Launch of Melksham Food and Drink Festival
Contrast
Variable Scope in C++
Discount Training Courses - PHP, Perl, Python
New C Examples - pointers, realloc, structs and more
I have not programmed before, and need to learn
2000th article - Remember the background and basics
How low can you sink?
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2004_Var ... in-C-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat