variable-scope

Scope of exception object in C++

What is the scope of the exception object in C++? does it go out of scope as soon as catch handler is executed? Also, if I create an unnamed exception object and throw it, then while catching that exception does it matter if I catch it by const reference or a non-const reference? ...

Closure problem? - passing current value of a variable

I'm trying to pass the current value of a variable when an a dynamically generated navigation 'node' is clicked. This needs to just be an integer, but it always results in the last node's value.. have tried some different methods to pass the value, a custom event listener, a setter, but I suspect it's a closure problem.. help would be ap...

JavaScript variable binding and loop.

Consider such loop: for(var it = 0; it < 2; it++) { setTimeout(function() { alert(it); }, 1); } The output is: => 2 => 2 I would like it to be: 0, 1. I see two ways to fix it: Solution # 1. This one based on the fact that we can pass data to setTimeout. for(var it = 0; it < 2; it++) { setTimeout(function(data...

Access variable from scope of another function?

<?php function foo($one, $two){ bar($one); } function bar($one){ echo $one; //How do I access $two from the parent function scope? } ?> If I have the code above, how can I access the variable $two from within bar(), without passing it in as a variable (for reasons unknown). Thanks, Mike ...

PHP recursive function + array by reference = headache

I have an interesting problem. The basis of the problem is that my last iteration of an array reference doesn't seem to "stick," if you will. A little context: I've devised a very simple data structure for page heirarchy that looks like this: ,1,2,3>,4>,5,6,7<<,8 Translation: forget about the annoying leading commas. Pages 1, 2, 3, & 8...

file scope and static floats

Hello there. I've run into an interesting problem in an AI project of mine. I'm trying to format some debug text and something strange is happening. Here's a block of code: float ratio = 1.0f / TIME_MOD; TIME_MOD is a static float, declared in a separate file. This value is modified based off of user input in another class (I hav...

How can I initialize lexical variables in a Perl while condition?

What is the proper way to write something equivalent to the follow: while ( my $first = $iterator->next && my $second = $iterator->next ) { # do work } This does not run - I wanted $first and $second in proper scope inside the while loop. ...

How to pass variables into inline functions in Action Script 2

I have the following function, but I can't seem to get the myVar variable into the inline function. What am I doing wrong here? What I would like to have happen is when I click on myMc, it should print myVar to the console ("hello computer"). function doSomething():Void { myVar = "hello computer"; myMc.onRelease = functio...

C++ header files simple question

Can .h files see what's in each other without being included? I know when I programmed in C before I could use variables in a .h file from other .h files without #include "myfile.h". I'm trying to do the same in C++ and I keep getting "definition out of scope error" ...

was not declared in this scope C++

Why do I get this error in the code below? class ST : public Instruction{ public: ST (string _name, int _value):Instruction(_name,_value){} void execute(int[]& anArr, int aVal){ //not implemented yet cout << "im an st" <<endl; anArr[value] = aVal; } virtual Instruction* Clone(){ return new ST(*this); ...

Javascript closure / variable scope question - I know it works, but why?

Hi, I've been developing with JS for a while, and while I know that code below works, I don't really understand why it works. The way I see it, I've defined testString in testClosure function, and I'm expecting that variable to 'go away' when testClosure function is done, since it's local variable. However, when I call inner functio...

Javascript variable scope in addEventListener anonymous function

When clicking on each div it should alert '1' if div 1 was clicked on or '5' if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application. <html> <head> <style type="text/css"> #div1 { background-color: #00ff00; margin: 10px; padding: 10px; } #div2 { background-color:...

Interacting with the JavaScript scope chain

Given the following snippet of javascript in a scope: var x = 10; function sayx() { alert(x); } sayx(); You would of course expect a message box printing '10', you could do multiple function nesting to interact with how 'x' is determined, because when resolving what x is the environment walks up the scope chain. You can even do a...

JavaScript: How is "function onload() {}" different from "onload = function() {}" ?

In the answers to this question, we read that function f() {} defines the name locally, while [var] f = function() {} defines it globally. That makes perfect sense to me, but there's some strange behavior that's different between the two declarations. I made an HTML page with the script onload = function() { alert("hello"); } and...

Portable equivalent to gcc's __attribute__(cleanup)

Recently I came across a gcc extension that I have found rather useful: __attribute__(cleanup) Basically, this allows you to assign a cleanup call to a local variable at the time it exits scope. For instance, given the following section of code, all memory must be maintained and handled explicitly in any and all cases within the call to...

JavaScript: Variable Value gets lost between functions

I have the following code function updateSliderContent(json) { //<- json defined here is correct var screen_order = json.screen_order.split('_'); jQuery.each(screen_order, function(i, item) { var screen_id = item; //at this point it is not, thus the function does not execute whatever is in the if blocks if (json...

Define constraints on the context in which as class is instantiated

I wonder if there's a way to define a class in such a way that instances of it will never be members of another class (only local variables), or the other way round - only members but never local. Is there any way in which a class can dictate the scope of it's prospective instances? ...

Illegal call to non-static member function (C++)?

Hi I'm developing a game which is based around the user controlling a ball which moves between areas on the screen. The 'map' for the screen is defined in the file ThreeDCubeGame.cpp: char m_acMapData[MAP_WIDTH][MAP_HEIGHT]; The ThreeDCubeGame.cpp handles most of the stuff to do with the map, but the player (and keyboard input) is co...

variable-variables inside function scope

Before you ask... I don't plan to actually do this. It's bad practice for obvious reasons. I'm just curious if it is possible. In javascript, you can use bracket syntax to make variable-variables in global scope: var var_name = 'my_var', var_value = 'my_value'; window[var_name] = var_value; alert( my_var ); // Works! alerts user...

PHP: Read a variable at top present at bottom

Hello All, Is there a way to read a variable at top which is defined some where at the bottom. Here is an example of such situation: <!--this image is somewhere on top of the page--> <img src="/<?php print logoPath(); ?>" border="0" /> // this code is somewhere on bottom of the page $logo_query = "select logo_path from table where id ...