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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:44:00+00:00 2026-05-11T08:44:00+00:00

I am working on a game and have an interesting question. I have some

  • 0

I am working on a game and have an interesting question. I have some game-wide constant values that I want to implement in one file. Right now I have something like this:

constants.cpp

extern const int BEGINNING_HEALTH = 10; extern const int BEGINNING_MANA = 5; 

constants.hpp

extern const int BEGINNING_HEALTH; extern const int BEGINNING_MANA; 

And then files just #include ‘constants.hpp’ This was working great, until I needed to use one of the constants as a template parameter, because externally-linked constants are not valid template parameters. So my question is, what is the best way to implement these constants? I am afraid that simply putting the constants in a header file will cause them to be defined in each translation unit. And I don’t want to use macros.

Thanks

  • 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. 2026-05-11T08:44:01+00:00Added an answer on May 11, 2026 at 8:44 am

    Get rid of the extern and you’re set.

    This code works perfectly fine in a header, because everything is ‘truly constant’ and therefore has internal linkage:

    const int BEGINNING_HEALTH = 10; const int BEGINNING_MANA = 5; const char BEGINNING_NAME[] = 'Fred'; const char *const BEGINNING_NAME2 = 'Barney'; 

    This code cannot safely be put in a header file because each line has external linkage (either explicitly or because of not being truly constant):

    extern const int BEGINNING_HEALTH = 10; extern const int BEGINNING_MANA = 5; const char *BEGINNING_NAME = 'Wilma';  // the characters are const, but the pointer isn't 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on game engine prototype and have the following question: Right now
OK so I am working on some game logic, I have done a fair
I am working on a game that will have a two dimensional board of
currently i am working in game application, Already i have one viewcontroller (and make
In my game I have a header file that contains properties and functions for
I'm working With Game Center right now and I have a issue with GC.
I am working on a 3d tile-based strategy game and have read that implementing
Alright, so I'm working on a game and I have found out that my
I've been working with some game engines lately. They all have an Update() function
I have a game app which is mostly working fine. If a phone call

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.