Declares extern const or just extern the same thing in the header file? Also will both give an external connection?
globals.cpp
#include <string> extern const std::string foo = "bar";
globals.h
#ifndef GLOBALS_H #define GLOBALS_H #include <iostream> extern const std::string foo; #endif /* GLOBALS_H */
OR
#ifndef GLOBALS_H #define GLOBALS_H #include <iostream> extern std::string foo; #endif /* GLOBALS_H */
Both compile and work fine, both give the same address when used in multiple files, which one is more correct?
It's right,
//globals.h extern const std::string foo; //Consistent
According!
You can give const an external connection, as in your example, but it is still not modifiable (permanent). Postscript you need to enable <string>, not<iostream>
<string>
<iostream>
PPS If I were unclear, you cannot override a constant. This will not compile:
extern std::string foo; extern const std::string foo = "bar";
-
extern const std::string foo;
foo - , , extern , , , foo.
foo
extern
, , , foo, , foo ( ). ( / - .:))
. , const, / , - foo.
( globals.cpp), globals.h foo (, "foo.c_str();" ), const std::string foo.
const std::string foo
. , foo, , . , globals.h, , globals.cpp; , const, , .
Source: https://habr.com/ru/post/1782456/More articles:Creating a JOGL window using NEWT - examples? - javaSet EntityCollection to Linq - linqHow to fill a string with a required character - stringHibernate Panel Cascade Remove Collection - javaC ++ on Windows: using DirectInput without a window? - c ++Multi-line text in SWT / WindowBuilder? - javaOracle global_names DELETE problem - sqlPython - default counter variable in for loop - pythonpython: closures and classes - pythonCustom Font Does Not Work in Firefox 3.6! - cssAll Articles