scope

Scope of variables in if statements

佐手、 提交于 2019-12-17 04:30:35
问题 I have a class that has no default constructor or assignment operator so it is declared and initialized within an if/else statement depending on the result of another function. But then it says that it is out of scope later even though both routes of the conditional will create an instance. Consider the following example (done with int just to illustrate the point): #include <iostream> int main() { if(1) { int i = 5; } else { int i = 0; } std::cout << i << std::endl; return 0; } Do variables

Is window really global in Javascript?

本秂侑毒 提交于 2019-12-17 04:29:15
问题 Take this piece of Javascript in a browser: <script> console.log(window.someThing); var x = 12; function foo() { window.otherThing = x; } </script> Inside foo we can access window , we all know that, but why exactly? Is it some kind of special global variable? Or does the "root scope" (inside the script tag) have it as an implicit local variable and is it simply "closure-inherited" as any other local variable (like x above) can be? And how does that concur with variables declared directly

Scope of exception object in C++

让人想犯罪 __ 提交于 2019-12-17 04:28:45
问题 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? 回答1: When a throw expression is evaluated, an exception object is initialized from the value of the expression. The exception object which is thrown gets its type from the static type of the throw expression ignoring

When is an object “out of scope”?

你说的曾经没有我的故事 提交于 2019-12-17 04:26:29
问题 In C++, when is an object defined as "out of scope"? More specifically, if I had a singly linked list, what would define a single list node object as "out of scope"? Or if an object exists and is being referenced by a variable ptr , is it correct to say that the object is defined as "out of scope" the moment the reference is deleted or points to a different object? UPDATE: Assuming an object is a class that has an implemented destructor. Will the destructor be called the moment the object

How can I return a variable from a $.getJSON function

ε祈祈猫儿з 提交于 2019-12-17 04:22:50
问题 I want to return StudentId to use elsewhere outside of the scope of the $.getJSON() j.getJSON(url, data, function(result) { var studentId = result.Something; }); //use studentId here I would imagine this has to do with scoping, but it doesn't seem to work the same way c# does 回答1: Yeah, my previous answer does not work because I didn't pay any attention to your code. :) The problem is that the anonymous function is a callback function - i.e. getJSON is an async operation that will return at

How can I return a variable from a $.getJSON function

一世执手 提交于 2019-12-17 04:22:43
问题 I want to return StudentId to use elsewhere outside of the scope of the $.getJSON() j.getJSON(url, data, function(result) { var studentId = result.Something; }); //use studentId here I would imagine this has to do with scoping, but it doesn't seem to work the same way c# does 回答1: Yeah, my previous answer does not work because I didn't pay any attention to your code. :) The problem is that the anonymous function is a callback function - i.e. getJSON is an async operation that will return at

Examples of the perils of globals in R and Stata

北战南征 提交于 2019-12-17 04:20:08
问题 In recent conversations with fellow students, I have been advocating for avoiding globals except to store constants. This is a sort of typical applied statistics-type program where everyone writes their own code and project sizes are on the small side, so it can be hard for people to see the trouble caused by sloppy habits. In talking about avoidance of globals, I'm focusing on the following reasons why globals might cause trouble , but I'd like to have some examples in R and/or Stata to go

Why is it OK to return a 'vector' from a function?

强颜欢笑 提交于 2019-12-17 04:12:52
问题 Please consider this code. I have seen this type of code several times. words is a local vector. How is it possible to return it from a function? Can we guarantee it will not die? std::vector<std::string> read_file(const std::string& path) { std::ifstream file("E:\\names.txt"); if (!file.is_open()) { std::cerr << "Unable to open file" << "\n"; std::exit(-1); } std::vector<string> words;//this vector will be returned std::string token; while (std::getline(file, token, ',')) { words.push_back

How to use objects from other namespaces and how to import namespaces in PHP

谁说我不能喝 提交于 2019-12-17 04:09:16
问题 What is the main difference between these two lines?: $obj = new ArrayObject(); & $obj = new \ArrayObject(); When I used the first line I got an error: "Fatal error: Class '\Foo\Bar\ArrayObject' not found..." and I am not too sure as to why I got this error? The second line seemed to have fixed the problem. 回答1: If you use: $obj = new ArrayObject(); it means that ArrayObject is defined in current namespace. You can use this syntax where you are in global namespace (no namespace defined in

How to use objects from other namespaces and how to import namespaces in PHP

心不动则不痛 提交于 2019-12-17 04:08:47
问题 What is the main difference between these two lines?: $obj = new ArrayObject(); & $obj = new \ArrayObject(); When I used the first line I got an error: "Fatal error: Class '\Foo\Bar\ArrayObject' not found..." and I am not too sure as to why I got this error? The second line seemed to have fixed the problem. 回答1: If you use: $obj = new ArrayObject(); it means that ArrayObject is defined in current namespace. You can use this syntax where you are in global namespace (no namespace defined in