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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:54:02+00:00 2026-06-15T21:54:02+00:00

I am trying to initialize an int with a particular value once only and

  • 0

I am trying to initialize an int with a particular value once only and use it repeatedly.

void some_method(int par){
    int ch=1;
  if (ch==1){
    int x = par;
  }
  int y = x + 2;
}

I know this code will definitely not work because x is only within the scope of the if statement. some_method() is called repeatedly in a while loop, so if I declare x outside the if statement then every time some_method() is called, default to zero. I just want x to remain constant. I thought about just making x a global variable, but I am sure there has got to be a better solution than that. Any suggestions will be helpful!

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. Editorial Team
    Editorial Team
    2026-06-15T21:54:03+00:00Added an answer on June 15, 2026 at 9:54 pm

    This can be done with static variables:

    void some_method(...)
    {
      static int initialized = 0, x;
    
      if(!initialized)
      {
        x = ...
        initialized = 1;
      }
      /* Use x. */
    }
    

    Note that you then need to “pay” for the check of initialized on every call, but if the work done to compute the value of x is expensive enough it might be worth it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to PInvoke into this C++ library function: int Initialize(Callback* callback); struct Callback
I have this structure which i am trying to initialize using the following code.
I'm trying to initialize a dictionary with string elements as keys and int[] elements
Can I declare an int array, then initialize it with chars? I'm trying to
I'm trying to initialize a Datatable with a default search value that the user
I'm trying to initialize string with iterators and something like this works: ifstream fin(tmp.txt);
I got this problem when trying to initialize global c++ matrix(2D array) inside a
This is probably the easiest/lamest question. So I am trying to initialize an array
I am trying to initialize a heap-allocated object as follows: class Ball { int
I'm trying to initialize inline an array of UInt16. For int I can do

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.