queries about the FAQ's section 7.5



 DEVELOP > c-Plus-Plus > queries about the FAQ's section 7.5

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "jimjim"
Date: 13 Sep 2005 11:30:11 AM
Object: 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-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
.

User: "Alf P. Steinbach"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 01:08:03 PM
* 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.
--
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?
.
User: "Ron Natalie"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 05:12:40 PM
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.
.
User: "Alf P. Steinbach"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 05:51:12 PM
* 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?
.
User: "jimjim"

Title: Re: queries about the FAQ's section 7.5 14 Sep 2005 04:34:06 AM
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:4327577b.247924859@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
.
User: "Alf P. Steinbach"

Title: Re: queries about the FAQ's section 7.5 14 Sep 2005 12:50:06 PM
* 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?
.


User: "Ron Natalie"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 08:52:48 PM
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.
.
User: "Alf P. Steinbach"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 10:40:19 PM
* 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?
.





User: "Peter Jansson"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 12:20:52 PM
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
.
User: "Howard"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 03:20:07 PM
"Peter Jansson" <webmaster@jansson.net> wrote in message
news:UTDVe.33675$d5.189054@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
.


User: "mlimber"

Title: Re: queries about the FAQ's section 7.5 13 Sep 2005 01:18:40 PM
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-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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER