Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:32:56+00:00 2026-06-02T05:32:56+00:00

In C++, if I want to define some non-local const string which can be

  • 0

In C++, if I want to define some non-local const string which can be used in different classes, functions, files, the approaches that I know are:

  1. use define directives, e.g.

    #define STR_VALUE "some_string_value"
    
  2. const class member variable, e.g.

    class Demo {  
    public:  
      static const std::string ConstStrVal;  
    };  
    // then in cpp  
    std::string Demo::ConstStrVal = "some_string_value";  
    
  3. const class member function, e.g.

    class Demo{  
    public:  
      static const std::string GetValue(){return "some_string_value";}  
    };  
    

Now what I am not clear is, if we use the 2nd approach, is the variable ConstStrVal always initialized to “some_string_value” before it is actually used by any code in any case? Im concerned about this because of the “static initialization order fiasco”. If this issue is valid, why is everybody using the 2nd approach?

Which is the best approach, 2 or 3? I know that #define directives have no respect of scope, most people don’t recommend it.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:32:57+00:00Added an answer on June 2, 2026 at 5:32 am

    if we use the 2nd approach, is the variable ConstStrVal always initialized to “some_string_value” before it is actually used by any code in any case?

    No

    It depends on the value it’s initialized to, and the order of initialization. ConstStrVal has a global constructor.

    Consider adding another global object with a constructor:

    static const std::string ConstStrVal2(ConstStrVal);
    

    The order is not defined by the language, and ConstStrVal2‘s constructor may be called before ConstStrVal has been constructed.

    The initialization order can vary for a number of reasons, but it’s often specified by your toolchain. Altering the order of linked object files could (for example) change the order of your image’s initialization and then the error would surface.

    why is everybody using the 2nd approach?

    many people use other approaches for very good reasons…

    Which is the best approach, 2 or 3?

    Number 3. You can also avoid multiple constructions like so:

    class Demo {
    public:  
      static const std::string& GetValue() {
        // this is constructed exactly once, when the function is first called
        static const std::string s("some_string_value");
        return s;
      }  
    };  
    

    caution: this is approach is still capable of the initialization problem seen in ConstStrVal2(ConstStrVal). however, you have more control over initialization order and it’s an easier problem to solve portably when compared to objects with global constructors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define some basic styles that I can combine. Let's say I
Can I some how make my non-exception objects become 'throwable' so that any local
I want to define some data in a row, which is master/detail in nature.
I want to define some member variable and some code just in Debug Mode,
I want to define some properties on a class using the [Indexable()] attribute in
I want to define some Contact Groups. I am wondering where and how does
I want to define some variables depend on whether it is run on Iphone
I want some examples of C preprocessor directives, such as: #define pi 3.14 #define
I have two non-overlapping sets of types and want to make other set which
I have a class IDocument which serve as a interface for some classes. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.