I am quite new to C++ and am currently taking a short-course on it. I have some background in Java.
I wish to have a namespace called "Message", that will be used to store unchanging/constant strings that will be used in a variety of different classes throughout my program. (E.g titles, keywords, names, etc).
If all of these strings were in a class, they would be const and static, thus I feel it is best to put them into a namespace rather than a class. My current "Message.h" looked a bit like this:
#ifndef MESSAGE
#define MESSAGE
#include <string>
namespace Message {
const std::string NAME = "Car";
const std::string SEPARATE = " | ";
const std::string COMMAND = "Please enter a 1, 2 or a 3: ";
};
#endif MESSAGE
Until an instructor suggested that I change it to this...
Message.h:
#ifndef MESSAGE
#define MESSAGE
#include <string>
namespace Message {
extern const std::string NAME;
extern const std::string SEPARATE;
extern const std::string COMMAND;
};
#endif MESSAGE
Message.cpp:
#include "Message.h"
const std::string Message::NAME = "Car";
const std::string Message::SEPARATE = " | ";
const std::string Message::COMMAND = "Please enter a 1, 2 or a 3: ";
I had little time for clarification from the instructor before the end of a session and it will be quite some-time before I get the opportunity to. From what I've researched, it has to do with translation-units and more specifically trying to use a variable in a different translation unit.
I understand the general concept of this, but what I can't quite catch is the benefits of using extern in this context?
Won't the include guards be enough here that the Message:: namespace variables won't be declared/defined more than once? Why is the extern keyword recommended in this context and is this purely for the benefits of compile-speed?