Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I came across a c++ code that creates static objects of a class.

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


I have a few questions
1.What is the use of static objects?
2.Since static is not in class scope ,how can we call functions of a class using static objects?
Posted
Updated 27-Feb-12 0:46am

static objects are useful in some scenarious. for example when u implement singleton pattern(where only one object of a class can be created) you can use static object like below
C++
class A
{
   private:
   A() {}
   public:
   static A* GetInstance()
   {
     static A a;
     return &a;
   }
}


hopes this is useful
jkchan
http://cgmath.blogspot.com
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 20:50pm    
This is just one of the forms of a singleton, and one of the possible uses, my 5.
I would add: 1) static members could also be used; 2) static members or variables are best avoided, but static class/structure functions are used everywhere; just the opposite; instance functions should not be used without real need to access "this" pointer.
--SA
In your code the objects are local static variables (see, for instance, here[^]).
That is the constructors (and destructors) of x1 and x2 are called once per application execution, no matter how many times the function f is called. Moreover, has stated by the documentation, the objects retains their states between calls to f.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 20:51pm    
My 5. Actually, I fail to understand OP's concern expressed in the question #2. He is probably missing something fundamental, hard to understand what.

Please see my comment to the question and my answer.
--SA
While the question #1 makes sense, #2 shows that you don't understand something about static, but I cannot understand what. Could you explain your concern? Do you see the difference between static members of class and static local objects you show in your code? Why do you think using them could be a problem?

You should understand that each such object is not created on stack or heap but is created only once per the process's runtime in some common fixed location. That is, the objects x1 and x2 are initialized only once; they are located in the same memory location of the process's memory, even if you call f again.

It means that you can, for example, accumulate the values in the static object from call to call. From the standpoint of syntax, these two objects are local to f, but from the standpoint of memory layout, they are outside, in some fixed memory location reserved for the static data of the project.

It is not clear what is your concern about using the class's methods. Which methods? You just need to take into account the above considerations; the calls themselves are… just calls, nothing special.

—SA
 
Share this answer
 
Comments
CPallini 28-Feb-12 3:22am    
My 5.
Sergey Alexandrovich Kryukov 28-Feb-12 12:36pm    
Thank you.
--SA
Abhinay Kumar 29-Feb-12 0:25am    
1.I have confusion in understanding why do we need static member functions?
2.What happens when we declare static member functions?
3.What does it means when static member functions have class scope or belong to class rather than objects of the class?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900