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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:38:14+00:00 2026-05-30T16:38:14+00:00

I have 2 questions: Why is this possible for an int variable: foo.h: class

  • 0

I have 2 questions:

Why is this possible for an int variable:

foo.h:

class foo{

     private:
        const static int a = 42;
};

but for a string variable I need to do it this way?

foo.h:

class foo{

     private:
        static string fooString;
};

foo.cpp:

string foo::fooString = "foo";

And also:

In my particular case foo::fooString should represent a path variable, and I would like that for every object of class foo there were just one instance of foo::string, representing a const value that should never change.

Is there another way to solve this problem?

  • 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-05-30T16:38:16+00:00Added an answer on May 30, 2026 at 4:38 pm

    Why is this possible for an int variable: [..] but for a string variable I need to do it this way?

    Just because. Actually you can still make the string const but, yes, you have to define it outside of the class definition. You can only do in-place initialisation of static members when they are const and integral (or “of literal type”).

    (In C++11 you can even do it for non-static non-const members when they are of literal type.)

    I would like that for every object of class foo there were just one instance of foo::string, representing a const value that should never change. Is there another way to solve this problem?

    A static const std::string, as you might expect.

    // C++03
    struct T {
       static const std::string foo;
    };
    
    const std::string T::foo = "lol";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen different questions regarding this, but I still find this topic to
Similar questions to this one have been asked but none seem to address my
I know questions of this kind have been asked before, but my situation differs
I know there have been some similar questions to this, but they haven't helped
I have looked through as many previous questions as possible but never saw a
Class: Class: private: ... vector<string> words; vector< list<int> > vints; public: myFunction(...) I am
I have a follow up question to this question . Is it possible to
This question is about App domains and Sessions. Is it possible to have IIS
I have 2 questions on this My code always seems to hit a 401
I have a few questions about this after reading the iPhone documentation on 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.