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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:59:51+00:00 2026-06-12T17:59:51+00:00

Possible Duplicate: file scope and static floats What are static variables? Here is a

  • 0

Possible Duplicate:
file scope and static floats
What are static variables?

Here is a code from a book.

class X
{
   int i;
public:
   X(int ii = 0) : i(ii) {cout<<i<<endl;} // Default
   ~X() { cout << "X::~X()" << endl; }
};
void f()
{
  static X x1(47);
  static X x2; // Default constructor required
}

int main()
{
  f();

   return 0;
}

My question is why would I like to declare an object as static like in function f()? What would happen if I did not declare x1 and x2 as static?

  • 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-12T17:59:53+00:00Added an answer on June 12, 2026 at 5:59 pm

    For this code it makes no difference to the observable behavior of the program.

    Change main to call f twice instead of only once, and observe the difference — if the variables are static then only one pair of X objects is ever created (the first time f is called), whereas if they’re not static then one pair of objects is created per call.

    Alternatively, change main to print something after calling f. Then observe that with static, the X objects are destroyed after main prints (the static objects live until the end of the program), whereas without static the objects are destroyed before main prints (automatic objects only live until exit from their scope, in this case the function f).

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

Sidebar

Related Questions

Possible Duplicate: Are file descriptors shared when fork()ing? Suppose I have following code in
Possible Duplicate: Get file name from URI string in C# How to extract file
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: Loop code for each file in a directory I assumed that I
Possible Duplicate: XNA - File not found problem Here I am trying to load
Possible Duplicate: File to byte[] in Java I want to read data from file
Possible Duplicate: include file from different directory I have a site laid out as
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: How to push file from PC to andriod phone programatically? I want
Possible Duplicate: Open a file in the proper encoding automatically my code: import csv

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.