declaration

What is the purpose of forward declaration?

妖精的绣舞 提交于 2019-11-27 15:43:07
what is the description or meaning of this: for example: class test; class test { ..... }; C++ (like C) was designed to be implementable by a single-pass compiler. Forward references are necessary in cases where the compiler needs to know that a symbol refers to a class before the class is actually defined. The classic example of this is when two classes need to contain pointers to each other. i.e. class B; class A { B* b; }; class B { A* a; }; Without the forward reference to B, the compiler could not successfully parse the definition for A and you can't fix the problem by putting the

Is there any difference between “Object[] x” and “Object x[]”?

怎甘沉沦 提交于 2019-11-27 15:34:36
I was updating a legacy code base in Java and I found a line like this: Object arg[] = { new Integer(20), new Integer(22) }; That line catched my attention because I am used to this kind of code: Object[] arg = { new Integer(20), new Integer(22) }; The content of the array isn't important here. I'm curious about the brackets next to the variable name versus the brackets next to the class name. I tried in Eclipse (with Java 5) and both lines are valid for the compiler. Is there any difference between those declarations? Both are legal and both work. But placing [] before the array's name is

Do inner functions in JavaScript get hoisted? How do the scoping rules apply to them?

房东的猫 提交于 2019-11-27 15:23:23
I thought that JavaScript doesn't have block scope, but function scope, and that declarations are hoisted from their block to the top of their parent functions. However, the following code does not work as expected: function one(a) { console.log("one called for " + a); if (a == 1) { function inner(b) { console.log("inner called for " + b); } inner(123); } inner(456); } one(1); one(2); one(3); The first one(1); call proceeds normally, without any errors, however the execution stops when the second one(2); is called. This behavior is intuitive: the function inner is defined only if a==1 . But

Syntax to define a Block that takes a Block and returns a Block in Objective-C

牧云@^-^@ 提交于 2019-11-27 15:14:02
问题 I find in Apple's document Working with Blocks that the syntax to define a block that returns the result of multiplying two values: double (^multiplyTwoValues)(double, double); is different than defining a block that takes another block as an argument and returns yet another block: void (^(^complexBlock)(void (^)(void)))(void); Why is the second syntax not void (^)(void)(^complexBlock)(void (^)(void)) ? 回答1: This is just how C syntax works. The Block syntax is based on that of function

C++ array size dependent on function parameter causes compile errors

萝らか妹 提交于 2019-11-27 14:39:47
I have a simple function in which an array is declared with size depending on the parameter which is int. void f(int n){ char a[n]; }; int main() { return 0; } This piece of code compiles fine on GNU C++ , but not on MSVC 2005. I get the following compilation errors: .\main.cpp(4) : error C2057: expected constant expression .\main.cpp(4) : error C2466: cannot allocate an array of constant size 0 .\main.cpp(4) : error C2133: 'a' : unknown size What can I do to correct this? (I'm interested in making this work with MSVC,without using new/delete) Jack Klein What you have found it one of the Gnu

When to use UIKIT_EXTERN vs just extern

為{幸葍}努か 提交于 2019-11-27 13:54:30
问题 I'm guessing I would only use UIKIT_EXTERN if there is a chance of C++ code in my project that may use the variable. If this is the case wouldn't it just be safe to declare all your externally available constants with UIKIT_EXTERN? How come I don't see this more? 回答1: I'm guessing I would only use UIKIT_EXTERN if there is a chance of C++ code in my project that may use the variable. Right. This is the primary reason. This happens because C and C++ symbols use different naming conventions.

Redeclaration of global variable vs local variable

て烟熏妆下的殇ゞ 提交于 2019-11-27 13:20: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 local variable is an error? In C, the statement int a; when made at file scope, is a declaration and a

How to read so many stars and parentheses in a templated function-pointer declaration? [duplicate]

隐身守侯 提交于 2019-11-27 13:13:26
问题 This question already has answers here : Complex C declaration (7 answers) Closed last year . From Introduction to the C++11 feature: trailing return types The article claims template <class T> class tmp { public: int i; }; auto foo()->auto(*)()->tmp<int>(*)(){ return 0; } is equivalent to template <class T> class tmp{ public: int i; }; tmp<int> (*(*foo())())() { return 0; } I don't understand the complex function in the second code example. Where should I look at in the beginning? I guess is

forward declaration of a struct in C?

耗尽温柔 提交于 2019-11-27 13:04:38
#include <stdio.h> struct context; struct funcptrs{ void (*func0)(context *ctx); void (*func1)(void); }; struct context{ funcptrs fps; }; void func1 (void) { printf( "1\n" ); } void func0 (context *ctx) { printf( "0\n" ); } void getContext(context *con){ con=?; // please fill this with a dummy example so that I can get this working. Thanks. } int main(int argc, char *argv[]){ funcptrs funcs = { func0, func1 }; context *c; getContext(c); c->fps.func0(c); getchar(); return 0; } I am missing something here. Please help me fix this. Thanks. Try this #include <stdio.h> struct context; struct

Where in a declaration may a storage class specifier be placed?

别来无恙 提交于 2019-11-27 12:57:42
问题 For example, let's consider the static storage class specifier. Here are a few examples of both valid and ill-formed uses of this storage class specifier: static int a; // valid int static b; // valid static int* c; // valid int static* d; // valid int* static e; // ill-formed static int const* f; // valid int static const* g; // valid int const static* h; // valid int const* static i; // ill-formed typedef int* pointer; static pointer j; // valid pointer static k; // valid (The declarations