C++ allows redefinition of global (const) variable?

badger5000

I'm a bit confused by global constants. My (beginner level) understanding is that 'global' variables are defined outside of a block and have program scope (source: http://www.learncpp.com/cpp-tutorial/42-global-variables/). But the program:

#include <iostream>

const double x=1.5;

int main(){
        std::cout << "1) x=" << x << std::endl;
        double x=2.5;
        std::cout << "2) x=" << x << std::endl;
        //const double x=3.5;
        return 0;
}

compiles in g++ (GCC, latest 64-bit version) without any problems, even with -Wall.

The output:

1) x=1.5
2) x=2.5

This is confusing to me. The fact that the first cout evaluates means that main recognises 'x' as a 'global' variable (it wasn't defined in main's scope). If that is the case, why does it let me redefine 'x'?

Then, if you uncomment the commented third declaration, g++ throws up a redeclaration error. Meaning, my first declaration can't have been 'global', in the sense I defined :S

edit: okay, question has nothing to do with global variables, but scopes: e.g same problem in http://pastebin.com/raw.php?i=V5xni19M

Angew is no longer proud of SO
#include <iostream>

const double x=1.5;

At this point in code, there is one object named x in the global scope, and it is of type const double.

int main(){
        std::cout << "1) x=" << x << std::endl;

At this point, there's still just one x visible (the global one), so that's what the name x refers to.

        double x=2.5;

At this point in code, you've introduced an object named x into the scope of main(). That scope is nested inside global scope, so now you have two objects named x:

  1. x in global scope of type const double

  2. x in the scope of main() of type double

The local x hides the global x. If you want to access the global x inside main(), you can refer to it as ::x.

    std::cout << "2) x=" << x << std::endl;
    double x=3.5;  //uncommented

No you're trying to introduce another object named x into the scope of main(). This is not possible, there already is one x in that scope, so it fails.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++: Redefinition of global variable

From Dev

Why Global variable redefinition is not allowed?

From Dev

Const variable in a global struct in C

From Dev

initialized vs uninitialized global const variable in c

From Dev

C++ redefinition of variable, global namespace polluted and I don't know why

From Dev

const global variable in namespace

From Dev

global const variable definition - access through extern in c++

From Dev

Redefinition of a pointer in global scope

From Dev

Avoid redefinition preprocessor variable

From Dev

Redefinition of a variable Error

From Dev

Python variable redefinition in a function

From Dev

Exporting global const variable from a DLL

From Dev

global definition of a variable in C

From Dev

Manipulation of global variable in C

From Dev

C++ const reference allows change from an expression?

From Dev

Does C allows non-const expressions in array initializer list?

From Dev

Does C allows non-const expressions in array initializer list?

From Dev

C++ const reference allows change from an expression?

From Dev

Redefinition error in c

From Dev

C - redefinition error in Xcode

From Dev

Issues by the redefinition of a macro in C

From Dev

C - redefinition error in Xcode

From Dev

Redefinition of a class C++

From Dev

C : redefinition of ‘struct'

From Dev

Redefinition; previous definition was 'data variable'

From Dev

Global const char pointer array in C++

From Java

CORE::GLOBAL::die redefinition inside BEGIN

From Dev

The address of const variable, C++

From Dev

unable to modify global variable in c