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

Error: (E107) bind interface to port failed: interface already bound to port: port 'MC8051_ALU().AM.port_27' (sc_in)
Error LINK2019 when I try to make a training for face detection [duplicate]
Quickest way to iterate in a C++ vector
How can I use snmp++ to enable/disable a switch port?
i wan't to use char* in this code instead of string how? [closed]
Using inRange() in OpenCV to detect colors in a range
The string.h 's function does not work in C++ but string does [on hold]
How to efficiently insert/merge into thrust::device_vector?
Change to transparent rectangle
Why addDays not working? [on hold]
OpenGL or GLFW renders ok only when moving the window
How to make static callback c++
How to copy host vector to device vector by thrust
conversion of string to const char*
Overloading structs with template call operator and generic lambdas - gcc vs clang
How can I make Selction row signal/slot in QT(QTableWidget)?

Categories

HOME
objective-c
arduino
google-analytics
slickgrid
rx-java
firebase-authentication
function
owasp
wildfly
boost
keyboard-shortcuts
virtocommerce
apache-apex
chapel
apostrophe-cms
bpm
lokijs
corba
spring4d
prestashop-1.6
j
web-component
markup
uidocumentinteraction
azure-storage-blobs
mitmproxy
fgets
mdm
filepicker.io
github-api
akka-stream
vowpalwabbit
jquery-chosen
aspose-cells
tornado-motor
javapoet
dexguard
filestream
james
audiokit
android-auto
model-driven
oracle-sql-data-modeler
trace
simplemodal
paypal-adaptive-payments
xunit.net
sp-executesql
android-datepicker
oltp
bisonc++
mongodb-scala
sceditor
collaborative-filtering
gyp
siteminder
rtai
ch
imshow
video-recording
jquery-widgets
xcode4
mindbody
yard
adobe-flash-cs3
jedit
stompjs
nameerror
xcode-bots
approximation
kermit
apache-roller
date-arithmetic
vtune
avaloq-script
tokumx
data-sharing
ios-provisioning
numpad
linq-to-lucene
string-conversion
complete.ly
rounded-corners
kendo-tooltip
aspnet-regiis.exe
java-service-wrapper
iwebkit
sequence-alignment
nsinteger
launching-application
testdriven.net
umbraco5
xcode4.1
equivalent
xmlnodelist
alasset
live-cd
progressive-enhancement
laconica
pvcs

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile