469,646 Members | 1,322 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

"static" question

Pat
I would like to know what is the meaning of :

static vector<int> v;

What is the difference with:

vector<int> v;
Jul 22 '05 #1
4 1371
* "Pat" <Pa*@Pat.com> schriebt:
I would like to know what is the meaning of :

static vector<int> v;

What is the difference with:

vector<int> v;


That depends on the context of the declaration.

If it is at namespace scope (outside any function or class) then 'static'
means that v has internal linkage, i.e., in practical terms, is not visible to
the linker outside the compilation unit.

If it is in a class or function then 'static' means that v has global
lifetime; for a class it is a single variable instead of a member variable in
each instance of the class; for a function it is a single variable instead of
an automatic variable created & destroyed for each call of the function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Pat
Thanks, Alf.

Pat
"Alf P. Steinbach" <al***@start.no> ¦b¶l¥ó
news:40****************@news.individual.net ¤¤¼¶¼g...
* "Pat" <Pa*@Pat.com> schriebt:
I would like to know what is the meaning of :

static vector<int> v;

What is the difference with:

vector<int> v;

That depends on the context of the declaration.

If it is at namespace scope (outside any function or class) then 'static'
means that v has internal linkage, i.e., in practical terms, is not

visible to the linker outside the compilation unit.

If it is in a class or function then 'static' means that v has global
lifetime; for a class it is a single variable instead of a member variable in each instance of the class; for a function it is a single variable instead of an automatic variable created & destroyed for each call of the function.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jul 22 '05 #3
Thats wrong. A static object of a class in a function will be constructed
only with the first call of the function and destructed only when the
program terminates although it might not be destructed with some abnormal
terminations.

Fraser.

"Alf P. Steinbach"
for a function it is a single variable instead of
an automatic variable created & destroyed for each call of the function.

Jul 22 '05 #4
[Do not top-post (I've rearranged this). Read the FAQ. Thanks in advance.]

* "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> schriebt:

* "Alf P. Steinbach"
for a function it is a single variable instead of
an automatic variable created & destroyed for each call of the function.
Thats wrong.


That it's wrong turns out not to be the case.

A static object of a class in a function will be constructed
only with the first call of the function
Not exactly. It's constructed the first time execution passes through
the declaration. Which might not be at the first call of the function,
or indeed ever.

Consider and try out:
#include <iostream>

struct X
{
X() { std::cout << "X constructed" << std::endl; }
~X() { std::cout << "X destroyed" << std::endl; }
};

void foo( int n )
{
std::cout << "foo( " << n << " )" << std::endl;
if( n == 3 )
{
static X anX;
}
}

int main()
{
for( int i = 1; i <= 5; ++i ) { foo( i ); }
}

and destructed only when the
program terminates although it might not be destructed with some abnormal
terminations.


Right.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Jahn Otto Næsgaard Andersen | last post: by
29 posts views Thread by Alexander Mahr | last post: by
3 posts views Thread by Ajax Chelsea | last post: by
3 posts views Thread by ruud.bos | last post: by
9 posts views Thread by Neil Kiser | last post: by
14 posts views Thread by Jess | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.