c++


Empty std::string in static method initialiser


I am using a static method to initialise the const fields of a class. The static method uses some const variables that are stored in a separate header file. Primitive types are correctly being passed to the static method, but the std::strings are being passed empty. I cannot understand why this is.
After doing some searching I have stumbled upon something called the static initialiser fiasco, but I'm having trouble wrapping my head around it, and can't work out if it is to blame. As the the object is in global scope, is the problem that it is being 'setup' before the std::string class has been 'setup'?
I have tried to replicate a minimal example below:
// File: settings.hpp
#include <string>
const std::string TERMINAL_STRING "Printing to the terminal";
const std::string FILE_STRING "Printing to a file";
// File: printer.hpp
#include <string>
#include <iostream>
class Printer
{
private:
const std::string welcomeMessage;
static std::string initWelcomeMessage(std::ostream&);
public:
Printer(std::ostream&);
}
extern Printer::print;
// File: printer.cpp
#include "settings.hpp"
std::string Printer::initWelcomeMessage(std::ostream &outStream)
{
if (&outStream == &std::cout)
{
return (TERMINAL_STRING);
}
else
{
return (FILE_STRING);
}
}
Printer::Printer(std::ostream &outStream) :
message(initWelcomeMessage(outStream)
{
outStream << welcomeMessage << std::endl;
return;
}
// File: main.cpp
#include "printer.hpp"
printer print(std::cout);
int main()
{
return (0);
}
Thanks very much!
As the the object is in global scope, is the problem that it is being 'setup' before the std::string class has been 'setup'?
Yes.
Have your strings be function-statics, returned by reference from some function, instead.
This is the traditional fix for the static initialisation order fiasco.

Related Links

getting “Unhandled exception” error after applying canny edge detector using opencv /c++
Editing a specific row in txt file using C++ [closed]
Static controls slightly flicker when main window is resized
Fibonacci - Divide and conquer algorithm [closed]
Handle “Out-of-Gamut” Color in RGB to CIEL*a*b* to RGB Conversions
Opengl show vertex in 2d or 3d
Finding exactly N consecutives in a sorted list
C++ bad_alloc caused by new?
PCL visualization in Qt using QVTK
cannot display the receive byte
best way to get the difference between two containers of type map< classId, set< studentId> >
Windows Speech API in Qt. Works in main(), but lots of errors when in class
Assertion fail with GetDC() (using mfc)
Assignment to a vector by index is not affecting its value after the loop
Diameter of a Tree
Intermediate Values and Thread Safety

Categories

HOME
google-maps-api-3
openerp
rx-java
angular-material2
statistics
bitcoin
keyboard-shortcuts
mutex
contiki
wayland
purescript
gemfire
spring-transactions
dijkstra
affinity
batch-normalization
dht
accurev
spring-statemachine
firebase-cloud-messaging
cider
mysql-error-1064
django-channels
eof
autoload
android-logcat
mahapps.metro
na
cad
multiple-domains
live
chmod
sublimerepl
gulp-4
tail-recursion
homescreen
android-auto
bspline
xilinx-edk
gemfile
multibox
grunt-string-replace
preferencefragment
gitversion
allegro5
generated
jsdoc3
equation
crypto++
implicit
bubble-sort
cscope
ldflags
windows-firewall-api
report-viewer2012
pdfobject
apple-watch-complication
nokia
correspondence-analysis
pxe
nsolid
kdiff3
f#-interactive
polymer-designer-tool
opcode
beta-testing
moovweb
pig-udf
dynamicquery
dcraw
file-attributes
chap-links-library
sqloledb
string-search
typescript1.4
automapper-3
stop-words
control-center
kiwi
impactjs
bitmapimage
updatemodel
yepnope
c++builder-6
android.mk
camping
clang-complete
custom-view
texinfo
soaplite
winqual
dft
installshield-2008
incremental-linking
application-design
white-box-testing
sqlmetal

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App