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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:05:44+00:00 2026-05-24T09:05:44+00:00

I googled const + extern on the internet, but it seems there isn’t really

  • 0

I googled const + extern on the internet, but it seems there isn’t really a good answer for my question.

const alone means internal linkage, but if I want to share a const variable among compilation units. Is extern the best choice?

Common solution would be:

//g.h
extern const int MAX;

// g.c
extern const int MAX = 3;

However, this solution has a drawback, like below:

// Say, I want to use this MAX in the same header file.
// g.h
extern const int MAX;
class AClass
{
public: 
    AClass (): i(MAX){}
private:
    int i;
};

Compiler will complain like:”error C2057: expected constant expression”.

Is there a solution for this?

  • 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-24T09:05:46+00:00Added an answer on May 24, 2026 at 9:05 am

    if you want to be able to use your constant at compile time (i.e. size an array by it, without using VLA) it has to be known at compile time, i.e. it cannot have external linkage.

    However, you could just declare your constant in your header file, and make it available to anyone including it. Still, that won’t have the exact same effect as an external linkage.

    // a.h
    const int MAX = 3;
    
    // a.cpp
    #include "a.h"
    int b[a];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's probably a Google search that'll answer this question but for the life of
I googled around and coulnd't really find an answer. How do I open an
I googled this a little but couldn't find a good result. Right now I'm
Googled everything, but can't find solution for my problem. When i'm trying to deploy
I googled for this for a while but can't seem to find it and
I googled for Drawing text on picturebox C# ,but I couldnt find anything useful.Then
I googled,I binged,I already have seen the other duplicates here,but none of them work
I googled and searched in the boost's man, but didn't find any examples. May
I googled around and I find a million results to this subject. But none
I searched a lot in google but not able to get the answer required.

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.