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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:05:31+00:00 2026-05-27T08:05:31+00:00

This is a general programming question. I’m learning about C++ and I’ve learned that

  • 0

This is a general programming question. I’m learning about C++ and I’ve learned that any const variables, ie: const int i, or int *const ptr, have to be initialized right away.

This is also the underlying reason that references to addresses must be initialized right away, because the addresses are const.

But I can’t find the reason why this must be done / why this rule is imposed.

Can anyone explain this for me please?

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

    Because there is no way you can initialize it, or assigned with a value, later on.

    const int size; //no initialization (error)
    
    size = 100; //error - you cannot assign a const variable.
    

    Now if a variable which is neither having any meaningful value, nor are you allowed to make it to have value later on because it is a const variable, then what is the point of such a variable? It is completely useless.

    However, this is true for only built-in and POD types:

    struct A{}; //POD type
    struct B{ B(){} }; //Non POD type because it has user-defined constructor!
    
    const int i; //error - built-in type
    const A a;   //error - POD type
    const B b;   //ok -    Non POD type
    
    //likewise
    const std::string s; //ok - std::string is a non-POD
    const std::vector<std::string> v; //ok - std::vector is a non-POD
    

    Actually a NON-POD type cannot remain uninitialized, because the default constructor will be called, and the object would get initialized.


    Now consider this struct,

    struct C
    {
       const int i;
       C() {}
    };
    

    C is definitely a non-POD type, because it has user-defined constructor. Also note that in the constructor, it doesn’t initialize i which is int, declared as const. Because of this uninitialized const i, the following would give error:

    const C c; //error - 
    

    One might think the error is because of const in the above declaration of variable c. But that is short-sightedness and is not true. Even if you remove const, it would give error:

    C c; //error - same error
    

    The error is because of C::i which is declared const but has not been initialized.

    Demo : http://ideone.com/NJT8L


    This analysis also demonstrates that built-in types do not get initialized automatically even if they’re members of non-POD types. This is true of non-POD class types as well.

    And the syntax to default initialization for built-in types (and POD types) is this:

    struct C
    {
        const int i;
        C() : i() {} //note the syntax - it is called member-initialization list
    };
    

    Now this is allowed :

    C x; //ok
    const C y; //ok
    

    Demo : http://ideone.com/84vD9


    As for what makes a struct/class POD, see this topic:

    • Can't C++ POD type have any constructor?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question about 3D programming in general, but I'm learning OpenGL if
Note: This is a general programming question, not specific to any language, but feel
This is a general programming question (it doesn't have a specific purpose/application yet). If
This is a general question on programming style. Let's say I have an object
This is a general database question, not related to any particular database or programming
This is a general programming question, not pertaining to any specific language. A new
Yesterday I asked this general question about decimals and their internal precisions. Here is
This is a general question about MVC as a pattern, but in this case
This is a general question about character encoding when using MD5 libraries in various
This is a general question of sorts, but do you think that it's important

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.