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

OpenCL returning -64 upon calling clGetPlatformIDs
Eclipse cdt debug quote error
How to stop main thread from another thread in c/c++
How can I use Blob Detector
C++ : for loop in a switch case
leveldb log_reader.h/Reader class design consideration
My linked lists program terminated with status -1073741819 In Code blocks compiler(x0000000005)) [closed]
Why does this double mutex lock not cause deadlock?
Helper function declaration outside class?
Auto declaration of map iterator error on GCC
Finding the length of a C style char * string in c++ [duplicate]
How to draw square on the button at the center using OpenGL?
How to use pthread to write a C++ class like “boost::shared_mutex”
Undefined reference to a superclass' virtual function
How many ways can fill in HxW rooms with 2x1 tatami mats? [closed]
CMake add_subdirectory not invoking the child CMakeLists.txt

Categories

HOME
node.js
java-ee
ruby-on-rails-4
solr6
servicestack
mips32
jboss-eap-6
put
chat
graphviz
tensorboard
rectangles
url-rewriting
osticket
vbulletin
delaunay
datepicker
junit4
emu8086
ietf-netmod-yang
clang-format
file-permissions
boto
jsx
indexoutofrangeexception
robotium-recorder
rubinius
hal
polymer-starter-kit
pdb-files
spring-4
grunt-connect-proxy
x509
slamdata
interactive
angular2-http
digestive-functors
ats
actor
dreamhost
google-console-developer
yield
cakephp-2.x
jedi-vim
docker-registry
same-origin-policy
android-gson
kubernetes-health-check
vqmod
ember-cli-mirage
rtai
azure-webjobssdk
type-systems
google-api-webmasters
gm
dde
correspondence-analysis
numerical-analysis
test-and-target
amazon-mobile-analytics
tvml
struts2-jquery-grid
stunnel
datamapper
singularitygs
command-line-tool
angularjs-ng-options
order-of-operations
wave
puphpet
setlocale
lightroom
xcode-organizer
cleartool
google-plugin-eclipse
httplib
swift-protocols
stop-words
rhodecode
psychology
svggraph
interlacing
zebra-puzzle
icommand
apprequests
jquery-ui-selectable
ldapconnection
pmp
keymando
html-validation
testdriven.net
sqa
web-setup-project
mindmapping
google-instant-previews
mahjong
language-theory
projective-geometry
maintenance-plan
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