P
Pramod
Hello to all of you,
I want to know that what's the use to create static object.
Thanks You
Pramod Sahgal
I want to know that what's the use to create static object.
Thanks You
Pramod Sahgal
Hello to all of you,
I want to know that what's the use to create static object.
Thanks You
Pramod Sahgal
Hi ,
The use of creation of a static object can be to create a connection
object for a database .
Regrads,
Ashish.
Pramod said:I want to know that what's the use to create static object.
Basically a static object is a global variable which visibility
is restricted to be inside the thing where it was declared. For
example, if you declare a static variable inside a class, the
lifetime of that static variable will be like the lifetime of a
global variable, but the scope variable will be limited to be inside
the class. In other words:
class A
{
static int i;
};
...
int A::i = 5;
Here 'i' will behave like a global variable, ie. it will be
initialized to 5 before main() is executed, and it will be
destroyed after main() has ended. It also behaves like a "global"
variable in that it's not bound to any specific instance of A,
but all the instances will use the one and same variable, and in
fact you can access it without an instance of A. The only difference
with a global variable is that 'i' is inside the scope of A and
can only be accessed through A (and it will not cause name collisions
outside of A).
A static variable inside a function is similar: It behaves like it
was a global variable, except that its visibility will be limited to
be inside that function. There's one other major difference with
global variables, though: It will be initialized the first time the
function is called (instead of some time before main() is called).
In practice this means that a static variable inside a function
will preserve its value between function calls.
For example:
void foo()
{
static int i = 0;
++i;
std::cout << i << std::endl;
}
'i' will behave like it was a global variable: The "++i" will
increment its value, and it will preserve that new value even after
the function is exited. The next time the function is called 'i' will
have the value 1 (which is then incremented to 2), and so on.
The initialization to 0 will be performed only once (not each time
the function is called).
The small twist is, as already mentioned, that 'i' will be initialized
the first time foo() is called. That allows doing something like:
void foo(int value)
{
static int i = value;
...
}
The 'i' variable will be initialized to the value of the function
parameter only the first time the function is called. Subsequent calls
will not modify it. (Naturally if you want it to initialize it each
time, obviously don't use 'static'.)
Why are static variables useful? Static member variables of a class
are called "class members". It's sometimes useful to have variables
which are common to all instances of a class, but which scope is
limited to be inside that class (so as to not to garbage the global
scope).
In my experience static variables inside functions are less useful
(mainly it's a historical feature from C) because usually what you
would want to do with a static function variable can be better done
with a class, but I suppose some people have found them useful too.
Basically a static object is a global variable which visibility
is restricted to be inside the thing where it was declared. For
example, if you declare a static variable inside a class, the
lifetime of that static variable will be like the lifetime of a
global variable, but the scope variable will be limited to be inside
the class. In other words:
class A
{
static int i;
};
...
int A::i = 5;
Here 'i' will behave like a global variable, ie. it will be
initialized to 5 before main() is executed, and it will be
destroyed after main() has ended. It also behaves like a "global"
variable in that it's not bound to any specific instance of A,
but all the instances will use the one and same variable, and in
fact you can access it without an instance of A. The only difference
with a global variable is that 'i' is inside the scope of A and
can only be accessed through A (and it will not cause name collisions
outside of A).
A static variable inside a function is similar: It behaves like it
was a global variable, except that its visibility will be limited to
be inside that function. There's one other major difference with
global variables, though: It will be initialized the first time the
function is called (instead of some time before main() is called).
In practice this means that a static variable inside a function
will preserve its value between function calls.
For example:
void foo()
{
static int i = 0;
++i;
std::cout << i << std::endl;
}
'i' will behave like it was a global variable: The "++i" will
increment its value, and it will preserve that new value even after
the function is exited. The next time the function is called 'i' will
have the value 1 (which is then incremented to 2), and so on.
The initialization to 0 will be performed only once (not each time
the function is called).
The small twist is, as already mentioned, that 'i' will be initialized
the first time foo() is called. That allows doing something like:
void foo(int value)
{
static int i = value;
...
}
The 'i' variable will be initialized to the value of the function
parameter only the first time the function is called. Subsequent calls
will not modify it. (Naturally if you want it to initialize it each
time, obviously don't use 'static'.)
Why are static variables useful? Static member variables of a class
are called "class members". It's sometimes useful to have variables
which are common to all instances of a class, but which scope is
limited to be inside that class (so as to not to garbage the global
scope).
In my experience static variables inside functions are less useful
(mainly it's a historical feature from C) because usually what you
would want to do with a static function variable can be better done
with a class, but I suppose some people have found them useful too.
Pramod said:int main()
{
static Foo *static_object;
I think I answered that question?
Hello Juha ,
Thanks for give me answer but my question is Static Object
class Foo
{
...
};
int main()
{
static Foo *static_object;
.........
.........
return 0;
}
What's the use to create this type of object?
Hello Juha ,
Thanks for give me answer but my question is Static Object
It does not seem to be wise to put static stuff in main function but aclass Foo
{
...
};
int main()
{
static Foo *static_object;
.........
.........
return 0;
}
What's the use to create this type of object?- Hide quoted text -
Pramod said:will you explain me again about static object?? please
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.