CodingMadeEasy
CodingMadeEasy

Reputation: 2337

I forgot what this is called?

Ok so I have this code and I KNOW it's not good programming practice. I just forgot what it's called.

int main()
{
    int variable; 
    {
        int variable;
    }
}

Is that a local namespace or something? I just can't remember the correct term for doing something such as that.

Upvotes: 1

Views: 170

Answers (6)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158599

There is two concepts going on here, one you are creating a block scope within the { ... } and you are hiding the outer instance of variable within this block scope. A block scope is being created by a compound statement(or block), if we look at the C++ draft standard section 6.3 Compound statement or block paragraph 1 has the following grammar:

compound-statement:
  { statement-seqopt}

it also says:

A compound statement defines a block scope (3.3).

and if we look at section 3.3.3 3.3.3 Block scope paragraph 1 says:

A name declared in a block (6.3) is local to that block; it has block scope. Its potential scope begins at its point of declaration (3.3.2) and ends at the end of its block. A variable declared at block scope is a local variable.

So what is happening with variable is that the local copy in the inner block is hiding the instance of variable in the outer block since it has the same name.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263587

The { ... } is a block or compound statement, which creates a nested scope. (It's not a namespace.)

This particular case, of a declaration in an inner scope having the same name as (and therefore hiding) a declaration in an outer scope is sometimes called shadowing.

g++ can warn about this. Quoting the manual:

`-Wshadow'
     Warn whenever a local variable or type declaration shadows another
     variable, parameter, type, or class member (in C++), or whenever a
     built-in function is shadowed. Note that in C++, the compiler will
     not warn if a local variable shadows a struct/class/enum, but will
     warn if it shadows an explicit typedef.

(As Adam Rosenfield points out in a comment, -Wshadow is not enabled by -Wall, -Wextra, or -pedantic; you have to enable it explicitly.)

Upvotes: 8

Prinz Km
Prinz Km

Reputation: 323

it is simply called scope of a variable.c permits us to define samevariable name variables .

Upvotes: 0

Mike Makuch
Mike Makuch

Reputation: 1838

I think what you're referring to is variable hiding, or variable shadowing. By declaring a variable in the inner scope with the same name as the outer scope, you are hiding the outer variable from the inner scope. Obviously in more complicated code this could be confusing.

http://en.wikipedia.org/wiki/Variable_shadowing

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234855

You are shadowing an existing variable. This is not good programming practice.

Out of interest, Java forbids it.

Upvotes: 2

user1356386
user1356386

Reputation:

I would call it a scope. I think that's what others call it too.

Upvotes: 0

Related Questions