Static Object

P

Pramod

Hello to all of you,

I want to know that what's the use to create static object.

Thanks You
Pramod Sahgal
 
R

Roy

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.
 
P

Pramod

Hi ,

The use of creation of a static object can be to create a connection
object for a database .

Regrads,

Ashish.

hello Ashish
not clear what do you want to say will you explain it.
 
J

Juha Nieminen

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.
 
P

Pramod

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.

Hello Juha Nieminen,
Thanks for give me reply...but my question is that Static object like

class Foo
{
 
P

Pramod

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.

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?
 
C

Colander

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?

First of all, you don't create an object in strict sense, your create
a pointer to an object that doesn't exists yet.

For the rest it's useless,unless you call main yourself in your code,
which I hope you don't. Static object are used to share (like globals)
between different pieces of code. But for main goes that is executed
once
so your variable is shared between all (that is one) instances of
main.
So if you leave out the static word nothing[1] will change.

Good luck,
Bas

[1] the time your pointer is created will change, but you will not
notice this.
 
T

terminator

Hello Juha ,

Thanks for give me answer but my question is Static Object

*object is somewhat a synonym for variable. you might mean a pointer I
guess.*
class Foo
{

...

};

int main()
{
static Foo *static_object;
.........
.........
return 0;

}

What's the use to create this type of object?- Hide quoted text -
It does not seem to be wise to put static stuff in main function but a
static pointer might aid some OS resource or some thing that does not
simply belong to the current process or an array of undeterminate size
or a null terminated C string,otherwise I would prefer a normal
variable(object).
 
O

osmium

Pramod said:
will you explain me again about static object?? please

I think the only part of the answer you got that applies in this example, is
that the system attempts to give Foo an initial value. This is because main
is a "special" function in that the only exit is at the end of the program's
life.

I suggest you post a less stripped down example of something that is claimed
by someone to do something useful and interesting, I would not use the word
"object" in the question. The more specific your question is, the more
useful the answer is likely to be. I skimmed the answer you were given and
it looks like it is probably a comprehensive answer. My guess is that it
was *too* comprehensive for what you really want to know - you don't know
what part of the answer to focus on.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top