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 8457347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:47:15+00:00 2026-06-10T12:47:15+00:00

Which is way to declare a string const is more recommended? Is to declare

  • 0

Which is way to declare a string const is more recommended?

  1. Is to declare the global variable at file scope.
  2. To declare it global to the class.

The variable will be used only in the class member functions. I am tending to feel 2 is better since it is specific to the class member functions only.

A.cpp
---------------------
static const std::string hello_str = "Hello";

void A::print()
{
    std::cout << hello_str;
}

(OR)

A.h
---------------------
class A{
public:
    static const std::string hello_str;
    void print();
}

A.cpp
---------------------
const std::string A::hello_str = "Hello";

void A::print()
{
    std::cout << A::hello_str;
}

Edit -1:

Let me say that the contents of hello_str can change. Eg. the string is updated manually by the developer whenever he makes a change to the file.

In this case, would it make sense to keep the variable initialization inside a function?
It may not be clear/evident for the user to update the string. If it was kept global to the file (1) or to the class (2) then other developers can “identify” & modify this string.

Given the above use case, do you still recommend having a function to return the string? Or can I use the class level static variable (with private access specifier)?

  • 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-10T12:47:16+00:00Added an answer on June 10, 2026 at 12:47 pm

    an anonymous namespace is another option:

    A.cpp

    namespace {
      const std::string hello_str("Hello");
    }
    
    void A::print() {
        std::cout << hello_str;
    }
    

    but you should really wrap that in a function for deferred initialization.

    that would take the form:

    A.h
    ---------------------
    class A{
    public:
        static const std::string& hello_str();
        void print();
    }
    
    A.cpp
    ---------------------
    const std::string& A::hello_str() {
      static const std::string str("Hello"); // << constructed on first call of A::hello_str()
      return str;
    }
    
    void A::print() {
        std::cout << A::hello_str();
    }
    

    in this case, you could also simply return by value, and avoid the static/global altogether. your std c++ library implementation may use what is call “small string optimization” — if so, no heap allocation would be required to create or move a string this short.

    also note that your two examples are not the same; one is effectively private, and the other is publicly visible.

    ultimately, you should use neither approach you have proposed. consider a static within a function for lazy initialization, or (even better in many cases) returning by value.

    to answer your original question: i favor the declaration inside the class, but private. i find this easier to maintain in the event implementations shift around. and of course, if that static in the cpp is somehow accessible to outside implementations, then you might want to also declare it as private inside the class so others cannot access it.

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

Sidebar

Related Questions

I want to know which is the preferred way to declare a string in
Is there any way to declare char variable (or maybe string) with the length
Is there a way to declare which signals are subscribed by a Python application
Which way is better for removing float decimals places or is there a more
Currently I have 2 ways of displaying images in a cell, which way will
The following code, in global scope, doesn't compile: const char *one = 1; const
What is a good way to cast an Ada String to a System.Adress which
which of the following is the preferred way to close and declare the inputStream.
Is there any way to declare a pointer to an incomplete type that will
i have a packet struct which have a variable len for a string example:

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.