declaration

Redefinition allowed in C but not in C++?

孤街醉人 提交于 2019-11-27 12:37:58
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 } Prasoon Saurav 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 , constitutes a tentative definition. If a translation unit contains one or more tentative

How to add constructors/destructors to an unnamed class?

折月煮酒 提交于 2019-11-27 11:55:59
问题 Is there a way to declare a constructor or a destructor in an unnamed class? Consider the following void f() { struct { // some implementation } inst1, inst2; // f implementation - usage of instances } Follow up question : The instances are ofcourse constructed (and destroyed) as any stack based object. What gets called? Is it a mangled name automatically assigned by the compiler? 回答1: You can not declare a constructor or destructor for an unnamed class because the constructor and destructor

What’s the difference between “{}” and “[]” while declaring a JavaScript array?

霸气de小男生 提交于 2019-11-27 11:34:22
What’s the difference between “{}” and “[]” while declaring a JavaScript array? Normally I declare like var a=[]; What is the meaning of declaring the array as var a={} Nobody seems to be explaining the difference between an array and an object. [] is declaring an array. {} is declaring an object. An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class. In fact, typeof [] === "object" to further show you that an array is an object. The additional

How should I use the “my” keyword in Perl?

痞子三分冷 提交于 2019-11-27 11:16:29
I keep seeing the "my" keyword in front of variable names in example Perl scripts online but I have no idea what it means. I tried reading the manual pages and other sites online but I'm having difficulty discerning what it is for given the difference between how I see it used and the manual. For example, its used to get the length of the array in this post: Find size of an array in Perl But the manual says: A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses. What does it do

Is is a good practice to put the definition of C++ classes into the header file?

我只是一个虾纸丫 提交于 2019-11-27 11:07:52
When we design classes in Java, Vala, or C# we put the definition and declaration in the same source file. But in C++ it is traditionally preferred to separate the definition and declaration in two or more files. What happens if I just use a header file and put everything into it, like Java? Is there a performance penalty or something? The answer depends on what kind of class you're creating. C++'s compilation model dates back to the days of C, and so its method of importing data from one source file into another is comparatively primitive. The #include directive literally copies the contents

Is there a use for function declarations inside functions?

夙愿已清 提交于 2019-11-27 09:12:56
We can declare functions inside functions (I wanted a local variable, but it parses as a function declaration): struct bvalue; struct bdict { bdict(bvalue); } struct bvalue { explict operator bdict() const; } struct metainfo { metainfo(bdict); } void foo(bvalue v) { metainfo mi(bdict(v)); // parses as function declaration metainfo mi = bdict(v); // workaround // (this workaround doesn't work in the presence of explicit ctors) } Are the sole reasons "because it makes the parser simpler" and "because the standard says so", or is there an obscure use for this? This is really a C question, because

Difference between int32, int, int32_t, int8 and int8_t

纵然是瞬间 提交于 2019-11-27 09:10:33
问题 I came across the data type int32_t in a C program recently. I know that it stores 32 bits, but don't int and int32 do the same? Also, I want to use char in a program. Can I use int8_t instead? What is the difference? To summarize: what is the difference between int32, int, int32_t, int8 and int8_t in C? 回答1: Between int32 and int32_t , (and likewise between int8 and int8_t ) the difference is pretty simple: the C standard defines int8_t and int32_t , but does not define anything named int8

Why are forward declarations necessary? [duplicate]

北战南征 提交于 2019-11-27 09:02:48
Possible Duplicate: Should C++ eliminate header files? In languages like C# and Java there is no need to declare (for example) a class before using it. If I understand it correctly this is because the compiler does two passes on the code. In the first it just "collects the information available" and in the second one it checks that the code is correct. In C and C++ the compiler does only one pass so everything needs to be available at that time. So my question basically is why isn't it done this way in C and C++. Wouldn't it eliminate the needs for header files? The short answer is that

Variables with the same name, but the local scope variable isn't being used, why?

断了今生、忘了曾经 提交于 2019-11-27 08:46:50
问题 Why is the declaration of the variable 'a' not used in the output of the console log of the below code? Doesn't that go against the scope chain? The variable 'a' with the undefined value on line 4 surely should be used on the console output on line 5? var a = 15; function checkScope(a) { var a; console.log(a); // log outputs 15 and not undefined } checkScope(a); I want to understand this behaviour. To confirm, this behaviour has nothing to do with hoisting or even scope (i.e. scope chain)? I

How to convert a K&R function declaration to an ANSI function declaration automatically?

女生的网名这么多〃 提交于 2019-11-27 08:06:22
问题 // K&R syntax int foo(a, p) int a; char *p; { return 0; } // ANSI syntax int foo(int a, char *p) { return 0; } As you see, in K&R style, the types of variables are declared in new lines instead of in the braces. How to convert a K&R function declaration to an ANSI function declaration automatically? Does anybody know such an easy-to-use tool in Linux? 回答1: You can use cproto or protoize (part of GCC) to generate function prototypes or convert old style (K&R) functions to ANSI format. 回答2: