c**********e 发帖数: 2007 | 1 extern const int MaxElements=10;
Which one of the following statements about the declaration in the sample
area above is true?
a) It defines a global constant variable that can be referenced from other
modules.
b) It references the global constant variable MaxElements declared in
another module.
c) It initializes a variable declared in another module.
d) It retrieves the global constant variable MaxElements declared in another
module.
e) It is only legal within an unnamed namespace.
(9_33 a32_48 |
x***y 发帖数: 633 | |
c**********e 发帖数: 2007 | 3 not right.
【在 x***y 的大作中提到】 : b
|
x***y 发帖数: 633 | 4 then d? I don't quite the difference between reference and retrieve...
【在 c**********e 的大作中提到】 : not right.
|
j***i 发帖数: 1278 | 5 I think is a
【在 x***y 的大作中提到】 : then d? I don't quite the difference between reference and retrieve...
|
x***y 发帖数: 633 | 6 define? Not sure, declare seems the right word..
【在 j***i 的大作中提到】 : I think is a
|
j***i 发帖数: 1278 | 7 as far as I remember, not sure
In c++ , all built-in declaration is a definition,
const will result internal linkage, but extern turn it to
external linkage.
【在 x***y 的大作中提到】 : define? Not sure, declare seems the right word..
|
c**********e 发帖数: 2007 | 8 This question demonstrates the use of extern and const together.
a) It defines a global constant variable that can be referenced from other
modules.
This is correct (C++ Standard 7.1.1/6). It defines a global constant variable that can be referenced from other modules.
b) It references the global constant variable MaxElements declared in
another module.
This is incorrect. The initializer makes it a definition.See other comments.
d) It retrieves the global constant variable MaxElements declared |
x***y 发帖数: 633 | 9 Yes, a is correct. I first thought this is used to refer to variables in
other modules....Actually, extern here seems not necessary...
variable that can be referenced from other modules.
comments.
another
【在 c**********e 的大作中提到】 : This question demonstrates the use of extern and const together. : a) It defines a global constant variable that can be referenced from other : modules. : This is correct (C++ Standard 7.1.1/6). It defines a global constant variable that can be referenced from other modules. : b) It references the global constant variable MaxElements declared in : another module. : This is incorrect. The initializer makes it a definition.See other comments. : d) It retrieves the global constant variable MaxElements declared
|
c**********e 发帖数: 2007 | 10 why extern not necessary? Doesn't it make the variable available to other
compilation unit?
【在 x***y 的大作中提到】 : Yes, a is correct. I first thought this is used to refer to variables in : other modules....Actually, extern here seems not necessary... : : variable that can be referenced from other modules. : comments. : another
|
|
|
s*********t 发帖数: 1663 | 11 I agree with you that extern is not necessary if a) is the answer here
【在 x***y 的大作中提到】 : Yes, a is correct. I first thought this is used to refer to variables in : other modules....Actually, extern here seems not necessary... : : variable that can be referenced from other modules. : comments. : another
|
M******q 发帖数: 94 | 12 I think extern is necessary. "const" by default has internal linkage by c++
rules. "extern" gives it external linkage so that other modules can access
the var.
【在 s*********t 的大作中提到】 : I agree with you that extern is not necessary if a) is the answer here
|
x***y 发帖数: 633 | 13 By default, the global variable can be shared by all the translate units
except you delcare it as static...
I verified in the programme again, and extern is not necessary....In my
opinion, extern is the storage specifier, as auto, register and static; and
const is the attribute specifier, as volatile, mutable....
【在 c**********e 的大作中提到】 : why extern not necessary? Doesn't it make the variable available to other : compilation unit?
|
t****t 发帖数: 6806 | 14 const makes namespace scope names internal linkage unless extern is
specified. (3.5 clause 3)
and
【在 x***y 的大作中提到】 : By default, the global variable can be shared by all the translate units : except you delcare it as static... : I verified in the programme again, and extern is not necessary....In my : opinion, extern is the storage specifier, as auto, register and static; and : const is the attribute specifier, as volatile, mutable....
|
x***y 发帖数: 633 | 15 Sorry, I made a mistake. You are right. when using const, we must have
extern to make it have the external linkage.
【在 t****t 的大作中提到】 : const makes namespace scope names internal linkage unless extern is : specified. (3.5 clause 3) : : and
|