473,471 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

queries about the FAQ's section 7.5

Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1. "In C, encapsulation was accomplished by making things static in a
compilation unit or module. "
I guess this refers only to static functions. As far as static variables are
concerned, they cannot be accessed by anywhere else apart from the
particular translation unit, unless declared as extern, right?

2. "By the way, static data at file-scope is now deprecated in C++: don't do
that"
Cant understand this!

3. What is the meaning of "Unfortunately this approach doesn't support
multiple instances of the data, since there is no direct support for making
multiple instances of a module's static data." Can you illustrate this point
with an example, please?

TIA
Sep 13 '05 #1
10 1300
jimjim wrote:
/.../
2. "By the way, static data at file-scope is now deprecated in C++: don't do
that"
Cant understand this!
My gues is that this means that you should not put e.g. "const int a=1;"
in a file at (file-scope). Rather, you should put the const int inside a
class or struct and use methods for accessing that const in from elsewhere.
3. What is the meaning of "Unfortunately this approach doesn't support
multiple instances of the data, since there is no direct support for making
multiple instances of a module's static data." Can you illustrate this point
with an example, please?


class test {
static int a;
int b;
};
static int test::a=1;

That's it, you have *one* instance of a, regardless of how many objects
of class test you create, observe that each new instance of test will
have new b's.

Regards,
Peter Jansson
Sep 13 '05 #2
* jimjim:
Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1. "In C, encapsulation was accomplished by making things static in a
compilation unit or module. "
I guess this refers only to static functions. As far as static variables are
concerned, they cannot be accessed by anywhere else apart from the
particular translation unit, unless declared as extern, right?

2. "By the way, static data at file-scope is now deprecated in C++: don't do
that"
Cant understand this!


It means that instead of C style

static int x = 666;
static void foo() {}

you should preferentially use a C++ style anonymous namespace,

namespace
{
int x = 666;
void foo();
}

Ours is not to ask why. But you can use the anonymous namespace version of
foo with the template mechanism. You cannot do that with the static version.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 13 '05 #3
jimjim wrote:
Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lit...s.html#faq-7.5

1. "In C, encapsulation was accomplished by making things static in a
compilation unit or module. "
I guess this refers only to static functions. As far as static variables are
concerned, they cannot be accessed by anywhere else apart from the
particular translation unit, unless declared as extern, right?
Well, variables can't be declared extern AND static simultaneously. You
have the principle right, however: static allows C to encapsulate
implementation behind its publicly available (i.e., extern-declared)
interface.
2. "By the way, static data at file-scope is now deprecated in C++: don't do
that"
Cant understand this!


Quoth the Stroustrup: "The use of static to indicate 'local to a
translation unit' is deprecated in C++. Use unnamed namespaces instead
(section 8.2.5.1)" (C++PL, section B.2.3).

In other words don't do this:

// File.cpp
static int i = 0;

Instead, do this:

// File.cpp
namespace {
int i = 0;
}

Unnamed namespaces have an implicit using directive and are unique
between translation units.

Cheers! --M

Sep 13 '05 #4

"Peter Jansson" <we*******@jansson.net> wrote in message
news:UT*******************@newsb.telia.net...
jimjim wrote:
/.../
2. "By the way, static data at file-scope is now deprecated in C++: don't
do that"
Cant understand this!


My gues is that this means that you should not put e.g. "const int a=1;"
in a file at (file-scope). Rather, you should put the const int inside a
class or struct and use methods for accessing that const in from
elsewhere.


That's not a static declaration. What's deprecated is static data at file
scope. You've simply declared a const int at file scope. (Did you actualy
mean to use "static" there, instead of "const"?)

-Howard
Sep 13 '05 #5
Alf P. Steinbach wrote:
It means that instead of C style

static int x = 666;
static void foo() {}

you should preferentially use a C++ style anonymous namespace,

Only the data statics are deprecated. There's no linkage issue
with functions.
Sep 13 '05 #6
* Ron Natalie:
Alf P. Steinbach wrote:
It means that instead of C style

static int x = 666;
static void foo() {}

you should preferentially use a C++ style anonymous namespace,
Only the data statics are deprecated.


Yes.

There's no linkage issue with functions.


Sorry, that's incorrect; see the post you replied to.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 13 '05 #7
Alf P. Steinbach wrote:
There's no linkage issue with functions.

Sorry, that's incorrect; see the post you replied to.

I know what I responded to, tell me how it's an issue for
functions. Data with non-external linkage is a template
issue. Functions themselves (rather than their types)
aren't.
Sep 14 '05 #8
* Ron Natalie:
* Alf P. Steinbach:
* Ron Natalie:

There's no linkage issue with functions.
Sorry, that's incorrect; see the post you replied to.

I know what I responded to, tell me how it's an issue for
functions.


See below.

Data with non-external linkage is a template issue.
?

Functions themselves (rather than their types) aren't.


template< void (*f)() >
struct Demo
{
static void callIt() { f(); }
};

namespace{ void foo() {} }
static void bar() {}

int main()
{
Demo<foo>::callIt(); // Nice doggie, nice!
Demo<bar>::callIt(); // Bad, bad dog.
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 14 '05 #9
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* Ron Natalie:
Alf P. Steinbach wrote:
> It means that instead of C style
>
> static int x = 666;
> static void foo() {}
>
> you should preferentially use a C++ style anonymous namespace,
>

Only the data statics are deprecated.


Yes.

There's no linkage issue with functions.


Sorry, that's incorrect; see the post you replied to.

I am quoting from another post in this conversation: "Quoth the Stroustrup:
"The use of static to indicate 'local to a
translation unit' is deprecated in C++. Use unnamed namespaces instead
(section 8.2.5.1)" (C++PL, section B.2.3)."
So, can static functions be used in C++? Or should they be wraped around an
anonymous namespace?

TIA
Sep 14 '05 #10
* jimjim:
I am quoting from another post in this conversation: "Quoth the Stroustrup:
"The use of static to indicate 'local to a
translation unit' is deprecated in C++. Use unnamed namespaces instead
(section 8.2.5.1)" (C++PL, section B.2.3)."
So, can static functions be used in C++?
Yes.

Or should they be wraped [in] an anonymous namespace?


"Should" is too strong a word, I think.

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

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

Similar topics

4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
2
by: Richard Cornford | last post by:
Anyone who has taken a look at the online FAQ today may have noticed that I have updated it. The majority of the changes are the updating of broken links and the implementation of that extensive...
89
by: Randy Webb | last post by:
There is an updated version of the FAQ at: <URL: http://jibbering.com/faq/newfaq/> The changes/modifications to date are: 2.3 Corrected "span" to "spam". 2.3 Updated with a note about not...
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
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,...
1
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.