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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:04+00:00 2026-05-22T00:54:04+00:00

Possible Duplicate: Static members class vs. normal c-like interface I am looking at somebodies

  • 0

Possible Duplicate:
Static members class vs. normal c-like interface

I am looking at somebodies code and there are several dozen constants defined in a class like this:

// header file

class Defines
{
public:
    static const int Val1;
    static const int ValN;
    static const char* String1;
    static const char* StringN;
...
}

// .CPP
const char* Defines::String1 = "some value"
etc.

Is there some reason to do things this was as opposed to using a namespace instead?
Are there advantages/disadvantages of one over the other

——— Edit ———-

I’m sorry, I obviously should have pointed this out explicitly, as nobody has inferred it from the name of the class – which is "Defines". i.e. these constants are not associated with a particular class, there has been a class created specifically just to hold constants and nothing else, that is all the class Defines contains.

My question is not why should you place constants in a particular class, the question is is there any value in collecting dozens of them together and placing them in a class whose only purpose is to collect together constants, as opposed to collecting them together in a namespace, or just collecting them together in a header file specifically for that purpose etc.

(There is no currently existing namespace in the project therefore potential issues of polluting the namespace as mentioned in answers are not relevant in this case.)

—– 32nd edit ———–

and a follow up question —
is placing
const char* Defines::StringN = "Somevalue"

in the .h file inefficient versus placing it in the .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-22T00:54:05+00:00Added an answer on May 22, 2026 at 12:54 am

    In general, there’s no reason to use a type this way. I have seen it argued that if the “collection of constants” evolves into a concrete object, starting this way makes the transition easier. In practice, I’ve never seen this happen. It just hides intent, and potentially flutters the code with private constructors.

    One could argue that classes work with templates, while namespaces do not. So something like the following would only work if Defines is a class:

    template<typename T> int function() {
        return T::x + T::y;
    }
    //later
    cout << function<Defines>() << function<OtherDefines>() << endl;
    

    In most cases, there’s probably a redesign that would work better, particularly if all you have are “constants” that aren’t really. Occasionally, this may come in handy, though.

    At times, it can also fight argument dependent lookup. In brief, the compiler is allowed to expand it’s search for eligable function names to different name namespaces based on the parameters passed to the function. This does not extend to static functions of a class. This applies more to the general case, though, where the “static class” also includes nested types and functions in addition to the collection of constants.

    Why people do this varies. Some come from languages where can’t be used this way, others just don’t know any better.

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

Sidebar

Related Questions

Possible Duplicate: What static analysis tools are available for C#? Guys, I'm looking for
Possible Duplicate Why can't I have a non-integral static const member in a class?
Possible Duplicate: static const vs #define In C++, to define a constant to be
Possible Duplicate: Are static inner classes a good idea or poor design? Java supports
Possible Duplicate: Can you make an Extension Method Static/Shared? Extension methods are great! Pardon
Possible Duplicate: How to tell whether I’m static or an object? Let's say I
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions

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.