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

  • Home
  • SEARCH
  • 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

I’m having some trouble with understanding how IIS is handling static variables on its
Why is the static keyword necessary at all? Why can't the compiler infer whether
Let's say I have a class designed to be instantiated. I have several private
I am building a small website for fun/learning using a fairly standard Web/Service/Data Access
Today I looked at the ZipEntry class and found the following: public class ZipEntry
I read somewhere that having public properties is preferable to having public members in
I have an application I am attempting to convert from a flex 3 air
The example program below compiles two in-memory assemblies. The first compilation works fine. The
I need to convert a (possibly) null terminated array of ascii bytes to a

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.