473,657 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 1531
* "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.i ndividual.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" <fraserATmember s.v21.co.united kingdom> 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
11732
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 instance of A is constructed: # a.h class A {
29
4387
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 reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
3
12360
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
1377
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
2304
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 that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
16
10811
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
2060
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
8858
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 created methods are not "registered" and only available through the DynamicMethod class. So a method lookup finds the origin property. A little test: public class DerivedClass : BaseClass {
14
6008
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.