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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:18:11+00:00 2026-05-28T06:18:11+00:00

i have a situation in C++ where i need to: 1) define a default

  • 0

i have a situation in C++ where i need to:

1) define a “default” value for static const variables in a namespace declared/defined in an object module

2) allow several “main” programs to “overwrite” these values using “custom” values

each “main” program has its own build folder and its own copy of the object module, so “main” programs are completely independent of each other:

e.g.

/mainProgramA/build/mainProgramA.o
/mainProgramA/build/Module.o
/mainProgramB/build/mainProgramB.o
/mainProgramB/build/Module.o

also, at any one time, either the default values or custom values will be defined/declared, but not both. if the compiler “finds” the custom values, they will be used, if not, the defaults will be used.

i can’t use weak symbols without namespaces because i need the namespaces to avoid naming clashes. i can’t use weak symbols with namespaces because i get “error: weak declaration of … must be public”. i could use a class perhaps, but all the values are known at compile time and not variable. What is the simplest and most elegant way to do this?

to clarify, i’m trying to do something like this:

DefaultValues.h:

namespace ConfigParams {
    static const int param1 = 1;
    static const int param2 = 2;
}

CustomValues.h:

namespace ConfigParams {
    static const int param1 = 100;
    static const int param2 = 200;
}

Module.h:

#include "DefaultValues.h"
class Module {
public:
    static void printParam1();
}   

Module.cpp:

#include "Module.h"

void Module::printParam1() 
{
    printf("%d\n", param1);
}

mainUsingDefaultValues.cpp (which will link in a copy of module.o where param1 == 1):

#include "Module.h"

...
Module::printParam1(); // Should print "1"

mainUsingCustomValues.cpp (which will link in a copy of module.o where param1 == 100):

#include "CustomValues.h"
#include "Module.h"

...
Module::printParam1(); // Should print "100"
  • 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-28T06:18:12+00:00Added an answer on May 28, 2026 at 6:18 am
    // File: CustomValues.h
    #define MY_CUSTOM_VALUES
    namespace ConfigParams 
    {
     static const int param1 = 100;
     static const int param2 = 200;
    }
    
    
    // File: DefaultValues.h
    #ifndef MY_CUSTOM_VALUES
    namespace ConfigParams 
    {
     static const int param1 = 1;
     static const int param2 = 2;
    }
    #endif
    

    You can make it more flexible if you need it.

    EDIT: Just in case I misunderstood the question. This way user of your library will be defaulted to DefaultValues.h, unless they include CustomValues.h before Module.h. You can have either one or another, but not both.

    #include "Module.h"
    #include "CustomValues.h"
    

    will not compile, since you’ll be redefining your constants.

    And just to make sure, you’ll need to include CustomValues.h in Module.cpp as well. So, probably, it’s better to make a #ifdef switch inside Module.h and make user #define MY_CUSTOM_VALUES somewhere before any Module.h includes:

    // File: Module.h
    #include "My_Constants.h" // #define MY_CUSTOM_VALUES can go here
    #ifdef MY_CUSTOM_VALUES
    #include "CustomValues.h"
    #elif
    #include "DefaultValues.h"
    #endif
    
    class Module
    {
     public:
      static void printParam1();
    }   
    // End of Module.h
    
    // File: Module.cpp
    #include "Module.h"
    
    void Module::printParam1() 
    {
     printf("%d\n", ConfigParams::param1);
    }
    // End of Module.cpp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I need to find the value with the key
I have this situation that I need to let users define decisions based on
If you have a situation where you need to know where a boolean value
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to be able to load assemblies in
I have a situation where I need to work with a datagrid and adding
I have a situation where i need to debug a Windows CE application in
I have a situation where I need to add an arbitrary unique id to
I have a situation where I need to do something like select 2, id
I have a situation where i need to enforce a unique constraint on 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.