1. 这个是我本来想在 stackoverflow 上面问的问题,不过上爆栈网的时候,chrome 突然说 javascript 什么的支持出问题了。最近也有些忙,先把这个问题记录下来吧。
Excuse me !
I ran into a puzzle when I was learning "return reference to an array"
this morning. Below there are descriptions of my problem.
using intArr = int[5];
/*
* function func listed below, intends to return a
* reference bound to the array
* given in the parameter list.
*/
intArr& func(int array[5]) {
/* simply returning array is incorrect because the parameter
will be implicitly converted into type int*. And it seems impossible
to cast int* into int[] which is not allowed by the compiler.
*/
return array; //invalid to convert int* into int[]
return reinterpret_cast<int[]>(array); //compile error -- not allowed
}
So how could I fix this problem ?
2. 假设有三个文件,一个头文件 test.h,一个源文件 test.cpp 是对 test.h 中的函数实现,一个源文件 main.cpp 中调用了 test.h 中声明的函数。test.cpp 中 include 了 test.h,main.cpp 也 include 了 test.h,那么分别编译之后,将test.o 和 main.o 链接成可执行文件的时候,该文件是否包含 test.h 中所有声明的两份 ?
我尝试过,在 test.h 中加入一个全局变量,link 的时候会出现 duplicated symbol 的错误,说明的确是包含了 test.h 中所有声明的两份。而且函数被多次声明也是可以的。虽然函数被多次声明不会出错,但是产生的 C++ 可执行文件不是会包含很多重复的函数声明,从而使可执行文件变大吗 ?