473,412 Members | 2,087 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,412 software developers and data experts.

Meaning of static object?

If a class defined as classA, what's the meaning of "static classA
objectA;", within a file scope or a function scope or a class member
function scope?

Many thanks!
Jul 22 '05 #1
2 1907
dover wrote:
If a class defined as classA, what's the meaning of "static classA
objectA;", within a file scope or a function scope or a class member
function scope?


It means that variable has scope limited to its location, but a lifespan
from before its first use to after its last use.

Each time control flow enters a scope where its available, and uses it, it
has the same identity and state as previous and subsequent times.

Tip: Give all identifiers the most restricted scope possible. This means
static objects at file scope are more restricted than static objects at
class scope.

Now look up "static function at file scope".

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
dover posted:
If a class defined as classA, what's the meaning of "static classA objectA;", within a file scope or a function scope or a class member function scope?

Many thanks!


class classA {};

--
static classA objectA;
--

File scope: the object can only be accessed from within
this particular file, this particular "translation unit",
which includes the header files includes the header files
"#include"ed into this file. It has the same effect when
applied to a function. Don't use "static" at file scope,
it's been superseded by "namespace".

Function scope: there's only one object. The object is
first created when the function is first called, but...
when the function finishes, the object lives-on to the next
function call. It's finally destroyed when the program
ends. Example:

unsigned char Blah()
{
static unsigned char times_called = 1;

return ++times_called;
}
Class scope: there's only one object. All instances of the
class, all objects created from the class are working with
the one sole object. As such, you don't even need an object
of the class to access the variable. "static" can also be
applied to a function in a class - this specifies that the
function does not need an actual object of the class to
work with.

#include <iostream>

class Blah
{
public:
static unsigned long blah;
char jack;

static unsigned long GetBlah()
{
//++jack; This line is illegal

return blah;
}
}

int main()
{
cout << Blah::GetBlah();
}
-JKop
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roman Simkin | last post by:
Hi, As far as I know, a static variable is a variable that belongs to a function or a class (are there any other options?). I've seen somewhere a function that *returns* static types - something...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
24
by: krishna | last post by:
Hi all, char a; will declare character array of size 10 starting with the index 0. what is the meaning of the following declaration? what is the purpose of such declarations? char a; ...
1
by: nas | last post by:
What is the meaning of this line?? static IFastString *(*pfnlr)(const char *)=0 and IFastString *(**ppfn)(const char *)=&pfnlr Here IFastString is the abstract class.
5
by: nas | last post by:
Hi..Please tell me what is the meaning of these lines static IFastString *(*pfnlr)(const char *)=0 and IFastString *(**ppfn)(const char *)=pfnlr Where IFastString is the abstract class
14
by: Summercool | last post by:
The meaning of a = b in object oriented languages. ==================================================== I just want to confirm that in OOP, if a is an object, then b = a is only copying the...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
33
by: James H. Newman | last post by:
I have a portion of code along the following lines: volatile unsigned char x ; unsigned int f(unsigned char *y) ; When I do unsigned int z = f(&x) ;
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.