mercredi 3 avril 2019

How to store string constants that will be accessed by a number of different classes?

There are too many different answers on stack overflow:

  1. Declare a namespace, and in the hpp file mark all strings as extern const, and in the cpp file put their definitions.

C++ How to share constants with extern between cpp - Error: storage class specified

  1. Use static const instead of extern const:

https://softwareengineering.stackexchange.com/questions/351827/is-it-bad-practice-to-define-constants-using-class-static-methods

  1. Use an inline function :

static string constants in class vs namespace for constants [c++]

  1. Use anonymous namespaces!

Where do I put constant strings in C++: static class members or anonymous namespaces?

This is all really confusing. Using an inline function seems like a really lengthy and tedious way to return a string constant.

Why can't we just use namespaces containing static strings, which are defined in the cpp file ?

Can someone please provide an categorical / unequivocal answer of how one should store strings in a constant file, so multiple cpp files can access them ?

Aucun commentaire:

Enregistrer un commentaire