Why does const imply C ++ internals when not in C?

See the topic. What were they thinking?

UPDATE: changed from “static” to “inner join” to save confusion.

To give an example ... Paste the following into a file:

const int var_a = 1; int var_b = 1; 

... and compiling with g++ -c test.cpp only exports var_b .

+72
c ++
Jun 15 '09 at 21:12
source share
7 answers

I suppose you mean

Why const implies internal communication in C ++

It is true that if you declare a const object in the namespace, it has an internal relationship.

Appendix C ( C ++ 11, C.1.2 ) provides the rationale

Change: the name of the file scope, which is explicitly declared as const, and not explicitly declared as extern, has an internal connection, while in C it will have an external connection

Rationale: Because const objects can be used as compile-time values ​​in C ++, this function encourages programmers to provide explicit initializer values ​​for each const. This function allows the user to place const objects in header files, which are included in many compilation modules.

+98
Jun 15 '09 at 21:21
source share

As indicated in litb, const objects have internal bindings. This is because they are intended to be used as follows:

 // a.cpp const int BUFSIZE = 100; char abuf[BUFSIZE]; // b.cpp const int BUFSIZE = 256 int bbuf[BUFSIZE]; 
+10
Jun 15 '09 at 21:27
source share

In C and C ++, the term static has several meanings (it can regulate communication and storage) You will need to read Stroustrup D & E to evaluate its justification, but when you declare a const variable in the namespace, it automatically has an internal connection, then as in C, you should declare it static in order to make it have an internal connection.

Of course, in C ++, using static to manage communications is outdated; anonymous namespaces can be used to simulate internal binding in C ++.

constant variables in C ++ were supposed to replace preprocessor constants - and since preprocessor constants are only visible in the files that define them, similarly, const automatically makes a variable visible only in the file that defines it.

+6
Jun 15 '09 at 21:27
source share

Const and static are orthogonal concepts in both C and C ++.

The const keyword tells the compiler to prevent the variable from being displayed as the lvalue of any expression - essentially making it read-only.

In C, the static has several uses, depending on what it applies to. When applied to a variable of a function, it indicates that the variable is not stored in the local scope of the function, but is accessible through its calls. When applied to a global variable or function, it becomes available only for a specific file - in other words, it is available only inside the compilation unit (unless extern declared).

In C ++, the static can be used in a class definition to make a variable or function shared in all instances of the class, rather than being local to each instance. In addition, the static function of a class in C ++ can access only the static variables of this class (or the classes to which it has access). Now in C ++ const it gives members an internal binding to the compilation module, if they are not explicitly declared extern - this may be what you refer to it. This allows you to share compile-time constants through a unit through the use of header files. Keep in mind, however, that members are not very static - rather, the constant is compiled into every place it refers to.

+5
Jun 15 '09 at 21:32
source share

These concepts are orthogonal and should not be regarded as one and the same.

Constness is an access property: it indicates only if your variable should be read-only (const) or write-read (not const).

Staticity is a property of life (and the technical localization of memory): it indicates whether the variable will be global in the class of the class (when in the class) or translation units (when used with the global variable defined in cpp).

+3
Jun 15 '09 at 21:18
source share

This is not so, and the most obvious example is that if you have a constant member variable (which is initialized by the constructor, of course), it is not shared by all objects of this class, but separate for each.

 class A { public: A(int newx) : x(newx); private int x; } 

litb gives the best answer above.

-2
Jun 15 '09 at 21:23
source share

This is not true. By writing the following:

 const int i = 0; 

doesn't make static (t) static (in C or C ++).

-four
Jun 15 '09 at 21:16
source share



All Articles