declaration

Redefinition allowed in C but not in C++?

我与影子孤独终老i 提交于 2019-11-26 18:12:39
问题 Why does this code work in C but not in C++? int i = 5; int i; // but if I write int i = 5; again I get error in C also int main(){ // using i } 回答1: Tentative definition is allowed in C but not in C++. A tentative definition is any external data declaration that has no storage class specifier and no initializer. C99 6.9.2/2 A declaration of an identifier for an object that has file scope without an initializer , and without a storage-class specifier or with the storage-class specifier static ,

How does the Java array argument declaration syntax “…” work?

十年热恋 提交于 2019-11-26 18:00:34
I have been writing java for a while, and today I encountered the following declaration: public static void main(String... args) { } Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything. So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare function like this, I can't declare an array within a function's body like this. Anyway, do you know

Why does “extern const int n;” not work as expected?

☆樱花仙子☆ 提交于 2019-11-26 17:43:11
My project consists of only two source files: a.cpp: const int n = 8; b.cpp: extern const int n; int main() { // error LNK2001: unresolved external symbol "int const n" (?n@@3HB) int m = n; } I know there are several methods to make it work; however, I just wonder WHY it does't work? It's because const implies internal linkage by default, so your "definition" isn't visible outside of the translation unit where it appears. In this case, by far the best solution is to put the declaration ( extern int const n; ) in a header file, and include that in both a.cpp and b.cpp . The linkage is

Private Methods in Objective-C, in Xcode 4.3 I no longer need to declare them in my implementation file ?

坚强是说给别人听的谎言 提交于 2019-11-26 17:26:37
问题 I have a lot question marks tolling above my head. What I don't get is before xcode 4.3 I needed to declare forward declarations (for private methods) in my implementation file. Like in my .m file: // deleting this with xcode 4.3 the below code still does work // in previous versions i had to put this because otherwise the compiler can't find methodFirst @interface DetailViewController () - (void)methodFirst; - (void)methodSecond; @end @implementation DetailViewController - (void)

What's the _ underscore representative of in Swift References?

冷暖自知 提交于 2019-11-26 17:03:22
In the reference section of Apple's docs there's lots of instances of this sort of thing: func runAction(_ action : SKAction!) The Objective-C 'equivalent' of this is: - (void)runAction:(SKAction *) action It strikes me that it's probably important that (in the Swift reference) there's a space after the underscore and "action" is written in italics. But I can't figure out what this is trying to convey. So perhaps the question is... is there a reference for the conventions used in the references? -- here's the page I'm referencing in this reference to the underscore use: https://developer.apple

Meaning of = delete after function declaration

夙愿已清 提交于 2019-11-26 16:55:13
class my_class { ... my_class(my_class const &) = delete; ... }; What does = delete mean in that context? Are there any other "modifiers" (other than = 0 and = delete )? Prasoon Saurav Deleting a function is a C++11 feature : The common idiom of "prohibiting copying" can now be expressed directly: class X { // ... X& operator=(const X&) = delete; // Disallow copying X(const X&) = delete; }; [...] The "delete" mechanism can be used for any function. For example, we can eliminate an undesired conversion like this: struct Z { // ... Z(long long); // can initialize with an long long Z(long) =

Redeclaration of global variable vs local variable

偶尔善良 提交于 2019-11-26 16:18:24
问题 When I compile the code below #include<stdio.h> int main() { int a; int a = 10; printf("a is %d \n",a); return 0; } I get an error: test3.c: In function ‘main’: test3.c:6:5: error: redeclaration of ‘a’ with no linkage test3.c:5:5: note: previous declaration of ‘a’ was here But if I make the variable global then it works fine. #include<stdio.h> int a; int a = 10; int main() { printf("a is %d \n",a); return 0; } Why is declaring the same global variable twice not an error, but doing that for a

Declaring and initializing variables within Java switches

送分小仙女□ 提交于 2019-11-26 16:18:06
I have a crazy question about Java switches. int key = 2; switch (key) { case 1: int value = 1; break; case 2: value = 2; System.out.println(value); break; default: break; } Scenario 1 - When the key is two it successfully print the value as 2. Scenario 2 - When I'm going to comment value = 2 in case 2: it squawks saying the The local variable value may not have been initialized . Questions : Scenario 1 : If the execution flow doesn't go to case 1: (when the key = 2 ), then how does it know the type of the value variable as int ? Scenario 2 : If the compiler knows the type of the value

Java, What is the difference between assigning null to object and just declaration

岁酱吖の 提交于 2019-11-26 16:14:06
问题 What is difference between : Object o = null ; and Object o; (just declaration) Can anyone please answer me? 回答1: It depends on the scope where you declare the variable. For instance, local variables don't have default values in which case you will have to assign null manually, where as in case of instance variables assigning null is redundant since instance variables get default values. public class Test { Object propertyObj1; Object propertyObj2 = null; // assigning null is redundant here

Strict mode in PHP?

北战南征 提交于 2019-11-26 15:50:40
问题 Other languages with automatic variable declaration - like Perl - have a strict mode. By activating this strict mode, variable declaration is required, and Perl throws an error as soon as you try to use an undeclared variable. Does PHP offer any similar feature? 回答1: Kind of. You can activate the E_NOTICE level in your error reporting. (List of constants here.) Every instance of usage of an undeclared variable will throw an E_NOTICE . The E_STRICT error level will also throw those notices, as