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

Avoiding virtual function calls in a computational graph
Null session pipes example, write access
Why does adding x + 1 skip 10?
Errors in prototype in C++ in XCode
C++ unordered set issue with struct hash [duplicate]
How to add cyclomatic complexity to sonar-cxx?
A pointer that can point to anywhere, how to determine if “delete” can be safely called on it?
Input will be floating value and program return whole part and decimal value
OpenDnp3 crosscompile tgo moxa UC8162LX segmentation fault at: epoll_reactor.hpp
Writing Memory to Process
Reading string from file
Is this a reasonably written test for a college 100-level software class midterm? [closed]
Wow64GetThreadContext error invalid parameter
Qt/C++ - Set background of disabled button?
Trouble with classes and adding objects
How to counting sort two dimensional array

Categories

HOME
loadrunner
phpunit
raster
amazon-dynamodb
rabbitmq
angular-material2
soap
set
datatable
raspberry-pi3
chat
glusterfs
playframework-2.5
widget
astyle
unity3d-unet
spring-data-elasticsearch
hudson
ajaxform
z80
android-appbarlayout
uilabel
live
puma
jung
nsdate
basic4android
leanft
james
youtube-analytics
r6
gobject
lift
binary-image
sw-precache
ssas-tabular
erpnext
nuodb
shodan
docker-registry
fixed
sceditor
countdowntimer
dde
mailman
nested-attributes
powergrep
pxe
idfa
testdroid
mediafire
customdialog
jquery-widgets
scrapyd
pdf-extraction
datamapper
zodb
cirqus
objdump
acronym
glut
order-of-operations
wave
seeding
mongojs
irssi
jquery-dropkick
slab
navigationbar
function-prototypes
arbitrary-precision
django-1.7
orca
deface
openacs
pixate
objectarx
raii
pyro
microsoft-search-server
presentmodalviewcontrolle
problem-steps-recorder
real-time-data
struts2-tiles-plugin
jnetpcap
django-staticfiles
array-formulas
static-array
mysql-error-1052
visual-studio-test-runner
ctime
32feet
dynamic-content
kiln
tao
vtl
httpverbs
id-generation
pia

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile