queries about the FAQ's section 7.5

J

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-lite/classes-and-objects.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
 
P

Peter Jansson

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
 
A

Alf P. Steinbach

* 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-lite/classes-and-objects.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.
 
M

mlimber

jimjim said:
Hello all,

I ve started reading the C++ FAQ. I cannot understand the concept of section
7.5:
http://www.parashift.com/c++-faq-lite/classes-and-objects.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
 
H

Howard

Peter Jansson said:
jimjim wrote:
/.../

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
 
R

Ron Natalie

Alf said:
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.
 
A

Alf P. Steinbach

* Ron Natalie:
Only the data statics are deprecated.
Yes.


There's no linkage issue with functions.

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

Ron Natalie

Alf said:
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.
 
A

Alf P. Steinbach

* Ron Natalie:
* Alf P. Steinbach:
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.
}
 
J

jimjim

Alf P. Steinbach said:
* Ron Natalie:

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
 
A

Alf P. Steinbach

* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top