Vacations are usually a good time to look back on the past. Here is a question that I was thinking about last month that made me "Ah-hah" for a moment.
Problem:
When you compile the following C + + program, you may think that the objects in the Modules (module) will be compiled and initialized first.
This assumption may not have the desired result.
X.h:
#include <string>
using namespace Std; The Translator notes: Add this line
Class Cobjet {
Public
static const string STRINGX;
};
X.cpp:
#include <X.h>
using namespace Std; Translator Note: This line can not
Const string COBJET::STRINGX = "001";
Y.cpp:
#include <iostream>
#include <X.h>
const string stringy= cobjet::stringx;
int main () {
cout << "Cobjet::stringx [" <<cobjet::stringx << "]" << Endl;
cout << "stringy [<< stringy <<"] "<< Endl;
return 0;
}
If you compile with the following command:
Xlc-c-i./X.cpp-o X.O
Xlc-c-i./Y.cpp-o y.o
Xlc-o binary X.O y.o