Thread: Can you make a variable const after its declaration?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    194

    Can you make a variable const after its declaration?

    Didn't find anything about this on Google. Is there no way to make a variable const after its declaration?

    For example I want to run a program get the value of a variable at the beginning and make the variable const thereon so it will not be modified by the program.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    799
    In C++ that's not possible, so you'd have to create a new variable and assign to that (or a const&). If you query the user, you might want to consider something like this:
    Code:
    UserData query_user()
    {
        UserData ud;
        // (...)
        return ud;
    }
    
    int main()
    {
        const auto user_data = query_user();
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,424
    But remember that that runtime "const" is not the same as a compile time const required for array sizes and the like.

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    Quote Originally Posted by -Adrian View Post
    In C++ that's not possible, so you'd have to create a new variable and assign to that (or a const&). If you query the user, you might want to consider something like this:
    Code:
    UserData query_user()
    {
        UserData ud;
        // (...)
        return ud;
    }
    
    int main()
    {
        const auto user_data = query_user();
    }
    I wanted to know if global variables who get values at run-time, once, can become const after getting their values. Thx anyways. Somebody needs to propose run-time constness.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    If your concern is for global variables in particular, then you can achieve this by using some variation of the singleton pattern in which the function providing the single global point of access returns a pointer or reference to const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    194
    I didn't understand a word. But are you talking about having a class with static member?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    Quote Originally Posted by Nwb
    I didn't understand a word. But are you talking about having a class with static member?
    It sounds like you need to read up then: I'm not going to explain the singleton pattern here when you could have googled it. It is hardly an obscure concept, especially since it has plenty of criticism in its usual form.

    The thing is that you apparently are not satisfied with doing this at file scope, even though it satisfies both "get the value of a variable at the beginning and make the variable const thereon" and your requirement for it to be global:
    Code:
    const auto user_data = query_user();
    This is reasonable though: query_user presumably should query the user at some point after program startup, whereas initialisation of user_data should happen at program startup (which might be acceptable for reading from a configuration file rather than querying the user). So, what I suggested is that you should be able to adapt some implementation of the singleton pattern to approximate this, i.e., by making sure that access will only result in a const reference or pointer to const, while allowing for the object to be populated well after program startup, perhaps at the time of first access (because the singleton class can control how that is to be done, just like it can control the very existence of one and only one object).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. const structure array declaration
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 10-28-2009, 08:18 PM
  2. Effect of const in a function declaration
    By Canadian0469 in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2008, 04:19 AM
  3. how to make a non const copy of a const variable?
    By sept in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2008, 02:11 AM
  4. Is it possible to make this int const?
    By pheres in forum C++ Programming
    Replies: 20
    Last Post: 10-23-2007, 03:03 PM
  5. const declaration help needed
    By penney in forum C Programming
    Replies: 1
    Last Post: 02-27-2003, 10:07 AM

Tags for this Thread