Local vs Global Variables

This is a discussion on Local vs Global Variables within the C++ Programming forums, part of the General Programming Boards category; Quick question. If I have a global variable and a local variable with the same name and I use the ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    Local vs Global Variables

    Quick question. If I have a global variable and a local variable with the same name and I use the variable which is used? Does the local variable take precedence or the global variable. Is 50 or 100 printed from the example below?

    Code:
    int x = 50;
    void foo()
    {
      int x = 100;
      cout<<x;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You get the local version by default. You can access the global version of x by using ::x instead of x (the :: is called the scope resolution operator).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In general, this should never occur. Your variables should have names such that a conflict between a local and a global variable cannot occur.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > Is 50 or 100 printed from the example below?
    Did you try it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    They really shouldn't have the same name, as was mentioned earlier, but if you wanted to find out do what Salem said ^^ there.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    6
    One thing though, as standard practice, you should never ever use a global variable. It tends to create the potential for your program's stability to be compromised. Attributes that belong to a class should always exist as private attributes, and you should only ever be able to access them via methods and friend functions.

    Although about which comes first.. Its all about scope. Which ever has the most recent scope (or the smaller scope I guess) Global has the scope of the entire class, next is method scope, then you can have if/while/for scopes. If you redefine it in the smallest end of the scope (which is the if/while/for), it'll take precedence over method and global. Hope that makes sense :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 08:01 AM
  2. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  3. Replies: 6
    Last Post: 01-02-2004, 12:01 PM
  4. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 09:53 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21