Questions tagged [scope]

Scope is an enclosing context where values and expressions are associated. Use this tag for questions about different types of scope as well for questions where scope may be unclear.

17524 questions
86
votes
3 answers

Derived template-class access to base-class member-data

This question is a furtherance of the one asked in this thread. Using the following class definitions: template class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) { /* do something for foo */ } T…
Shamster
  • 2,092
  • 5
  • 24
  • 27
85
votes
6 answers

Why can't we define a variable inside an if statement?

Maybe this question has been answered before, but the word if occurs so often it's hard to find it. The example doesn't make sense (the expression is always true), but it illustrates my question. Why is this code valid: StringBuilder sb; if ((sb =…
comecme
  • 6,086
  • 10
  • 39
  • 67
85
votes
11 answers

What do curly braces in Java mean by themselves?

I have some Java code that uses curly braces in two ways // Curly braces attached to an 'if' statement: if(node.getId() != null) { node.getId().apply(this); } // Curly braces by themselves: { List copy = new…
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
83
votes
3 answers

Use external variable in array_filter

I've got an array, which I want to filter by an external variable. The situation is as follows: $id = '1'; var_dump($id); $foo = array_filter($bar, function($obj){ if (isset($obj->foo)) { var_dump($id); if ($obj->foo == $id)…
Sander Koedood
  • 6,007
  • 6
  • 25
  • 34
82
votes
4 answers

Variable scopes in Python classes

Declaring a variable in a class (outside of a function): all class functions can access it (basically a public variable) Declaring a variable inside a function inside a class: only that function can access it (it's in that function's…
jason
  • 4,721
  • 8
  • 37
  • 45
82
votes
2 answers

Scope of variable within "with" statement?

I am reading only firstline from python using : with open(file_path, 'r') as f: my_count = f.readline() print(my_count) I am bit confused over scope of variable my_count. Although prints work fine, would it be better to do something like…
danish sodhi
  • 1,793
  • 4
  • 21
  • 31
81
votes
3 answers

Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)

Program A() { x, y, z: integer; procedure B() { y: integer; y=0; x=z+1; z=y+2; } procedure C() { z: integer; procedure D() { x: integer; x = z…
petrov
  • 1,085
  • 3
  • 12
  • 13
80
votes
5 answers

Scope issues with Angular UI modal

I'm having trouble understanding/using the scopes for an angular UI modal. While not immediately apparent here, I have the modules and everything set up correctly (as far as I can tell), but these code samples in particular are where I'm finding…
coblr
  • 3,008
  • 3
  • 23
  • 31
79
votes
4 answers

Python overwriting variables in nested functions

Suppose I have the following python code: def outer(): string = "" def inner(): string = "String was changed by a nested function!" inner() return string I want a call to outer() to return "String was changed by a nested…
Ord
  • 5,693
  • 5
  • 28
  • 42
79
votes
4 answers

Private inner classes in C# - why aren't they used more often?

I am relatively new to C# and each time I begin to work on a C# project (I only worked on nearly mature projects in C#) I wonder why there are no inner classes? Maybe I don't understand their goal. To me, inner classes -- at least private inner…
Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
79
votes
1 answer

Nested templates with dependent scope

What is dependent scope and what is the meaning of typename in the context of the following error? $ make g++ -std=gnu++0x main.cpp main.cpp:18:10: error: need 'typename' before 'ptrModel >::Type' because…
user383352
  • 5,033
  • 4
  • 25
  • 20
79
votes
6 answers

Mixing extern and const

Can I mix extern and const, as extern const? If yes, does the const qualifier impose it's reign only within the scope it's declared in or should it exactly match the declaration of the translational unit it's declared in? I.e. can I declare say…
legends2k
  • 31,634
  • 25
  • 118
  • 222
78
votes
6 answers

In Python 2, how do I write to variable in the parent scope?

I have some code like: def example(): # other logic omitted stored_blocks = {} def replace_blocks(m): block = m.group(0) block_hash = sha1(block) stored_blocks[block_hash] = block return '{{{%s}}}' %…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
76
votes
2 answers

What do the curly braces do in switch statement after case in es6?

What's the difference between: switch (expression) { case: somethings; break; } and switch (expression) { case: { somethings; break; } } At first I thought that I could return an object literal like so, but it…
Aurimas
  • 2,577
  • 5
  • 26
  • 37
75
votes
1 answer

What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?

In VB6/VBA, you can declare module-level variables outside of a specific Sub or Function method. I've used Private and Public before inside modules and understand them like so: Public - visible to all code inside the module and all code outside…
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223