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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:55:29+00:00 2026-05-12T07:55:29+00:00

In C++, is there any reason to not access static member variables through a

  • 0

In C++, is there any reason to not access static member variables through a class instance? I know Java frowns on this and was wondering if it matters in C++. Example:

class Foo {
  static const int ZERO = 0;
  static const int ONE = 1;
  ...
};


void bar(const Foo& inst) {
   // is this ok?
   int val1 = inst.ZERO;
   // or should I prefer:
   int val2 = Foo::ZERO
   ...
};

I have a bonus second question. If I declare a static double, I have to define it somewhere and that definition has to repeat the type. Why does the type have to be repeated?
For example:

In a header:
  class Foo {
    static const double d;
  };
In a source file:
  const double Foo::d = 42;

Why do I have to repeat the “const double” part in my cpp file?

  • 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-12T07:55:30+00:00Added an answer on May 12, 2026 at 7:55 am

    For the first question, aside from the matter of style (it makes it obvious it’s a class variable and has no associated object), Fred Larsen, in comments to the question, makes reference to previous question. Read Adam Rosenthal’s answer for very good reason why you want to be careful with this. (I’d up-vote Fred if he’d posted it as answer, but I can’t so credit where it’s due. I did up-vote Adam.)

    As to your second question:

    Why do I have to repeat the “const double” part in my cpp file?

    You have to repeat the type primarily as an implementation detail: it’s how the C++ compiler parses a declaration. This isn’t strictly ideal for local variables either, and C++1x (formerly C++0x) makes use of the auto keyword to avoid needing to be repetitive for regular function variables.

    So this:

    vector<string> v;
    vector<string>::iterator it = v.begin();
    

    can become this:

    vector<string> v;
    auto it = v.begin();
    

    There’s no clear reason why this couldn’t work with static as well, so in your case thos:

    const double Foo::d = 42;
    

    could well become this.

    static Foo::d = 42;
    

    The key is to have some way of identifying this as a declaration.

    Note I say no clear reason: C++’s grammar is a living legend: it is extremely hard to cover all of its edge cases. I don’t think the above is ambiguous but it might be. If it isn’t they could add that to the language. Tell them about it … for C++2x :/.

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

Sidebar

Related Questions

Is there any reason why this does not work on Internet Explorer or Chrome:
For 'large' tables, is there any reason not to put a filter on indexes
Is there any reason why images are not being displayed if I place it
Is there any reason for my jQuery effects not to be taking effect immediately?
Is there any reason for automating tests that focus on GUI (and not to
HD7970 vs FirePro V8800 Is there any reason why I should not go with
Other than convention, is there any reason to use or not to use local
Possible Duplicate: Is there a reason you can not define the access modifier on
Is there any viable reason why serialVersionUID field is not named SERIAL_VERSION_UID? According to
Is there any particular reason to use memcached for fast access to cached data

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.