473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

"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 1512
* "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jahn Otto Næsgaard Andersen | last post by:
Hi, I have a class A with a static pointer to some data. The pointer is static because I want to access the data from all instances of the class. The data itself is allocated when the first...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
3
by: ruud.bos | last post by:
Hi list, As a C++ newbie, I have a question about static member functions. Suppose I have the following class definition: class MyClass { public: static void MyFunc(); };
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
16
by: Chris | last post by:
Looking at some code I see a declaration inside a function like static const string s("some string"); Does the static serve any purpose here?
9
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.