* "static const" attribute in C++ (How to ?)
@ 2004-06-28 21:42 Luciano Moreira - igLnx
2004-06-28 21:52 ` Glynn Clements
2004-06-29 1:10 ` Micha Feigin
0 siblings, 2 replies; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-06-28 21:42 UTC (permalink / raw)
To: linux-c-programming
Sorry for this rookie question, but I'm not a experienced C++ programmer
(I m a old C-ANSI programmer, not in C++)
How can us create a kind of "static const" attribute into a class ? I
need to use it like as:
char myArray[MYCLASS::DEFSIZE];
Where "DEFSIZE" whould be a "static const" member (or another type) of
MYCLASS.
What do I already tried ? Follow:
class MYCLASS {
public:
static const int DEFSIZE;
};
const int DEFSIZE = 100;
But, when the file "MYCLASS.h" is included more than one file ".cpp",
the linker says that the member "MYCLASS::DEFZISE" "was already defined
in 'filename'". And don't have occurrences of compiler errors, only on
linking.
Thanks,
Luciano
-------------------------------------------
FOLLOW SOME COMPILER FULL ERROR MESSAGE (MSVC++ 6.0):
HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned
int const MMDadosCarteira::_OFFSET_CARTEIRA2B"
(?_OFFSET_CARTEIRA2B@MMDadosCarteira@@2IB) already defined in HMApp.obj
HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned
int const MUDadosRecarga::_OFFSET_CARTEIRA2B"
(?_OFFSET_CARTEIRA2B@MUDadosRecarga@@2IB) already defined in HMApp.obj
PS: I didn't code "static unsigned int const". In my source code I coded
"static const unsigned" only. I don't know why MSVC "translated" my code.
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-28 21:42 "static const" attribute in C++ (How to ?) Luciano Moreira - igLnx
@ 2004-06-28 21:52 ` Glynn Clements
2004-06-29 1:10 ` Micha Feigin
1 sibling, 0 replies; 28+ messages in thread
From: Glynn Clements @ 2004-06-28 21:52 UTC (permalink / raw)
To: Luciano Moreira - igLnx; +Cc: linux-c-programming
Luciano Moreira - igLnx wrote:
> Sorry for this rookie question, but I'm not a experienced C++ programmer
> (I m a old C-ANSI programmer, not in C++)
>
> How can us create a kind of "static const" attribute into a class ? I
> need to use it like as:
> char myArray[MYCLASS::DEFSIZE];
That won't work. Array sizes have to be constant expressions
(expressions which contain only literal values).
> Where "DEFSIZE" whould be a "static const" member (or another type) of
> MYCLASS.
>
> What do I already tried ? Follow:
> class MYCLASS {
> public:
> static const int DEFSIZE;
> };
> const int DEFSIZE = 100;
>
> But, when the file "MYCLASS.h" is included more than one file ".cpp",
> the linker says that the member "MYCLASS::DEFZISE" "was already defined
> in 'filename'". And don't have occurrences of compiler errors, only on
> linking.
Definitions belong in source (.cpp) files, not header files.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-28 21:42 "static const" attribute in C++ (How to ?) Luciano Moreira - igLnx
2004-06-28 21:52 ` Glynn Clements
@ 2004-06-29 1:10 ` Micha Feigin
1 sibling, 0 replies; 28+ messages in thread
From: Micha Feigin @ 2004-06-29 1:10 UTC (permalink / raw)
To: linux-c-programming
On Mon, Jun 28, 2004 at 06:42:07PM -0300, Luciano Moreira - igLnx wrote:
> Sorry for this rookie question, but I'm not a experienced C++ programmer
> (I m a old C-ANSI programmer, not in C++)
>
> How can us create a kind of "static const" attribute into a class ? I
> need to use it like as:
> char myArray[MYCLASS::DEFSIZE];
> Where "DEFSIZE" whould be a "static const" member (or another type) of
> MYCLASS.
>
> What do I already tried ? Follow:
> class MYCLASS {
> public:
> static const int DEFSIZE;
> };
> const int DEFSIZE = 100;
>
The line setting the value should in a .cpp file not a header. You may
only set the value once for a variable during compilation (the value is
put in the actual file, not generated on startup - the data section)
I am not sure if a static const may be used for array sizes? IIRC it is
possible under c++ and not on c but far away from sure on that.
Another option is to define the value as an enum and then you still get
type checking and you can encapsulate it in a class unlike a define,
but it acts like a define for all other purposes (you can use it as a
test option for a case statement).
> But, when the file "MYCLASS.h" is included more than one file ".cpp",
> the linker says that the member "MYCLASS::DEFZISE" "was already defined
> in 'filename'". And don't have occurrences of compiler errors, only on
> linking.
>
> Thanks,
>
> Luciano
>
> -------------------------------------------
> FOLLOW SOME COMPILER FULL ERROR MESSAGE (MSVC++ 6.0):
> HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned
> int const MMDadosCarteira::_OFFSET_CARTEIRA2B"
> (?_OFFSET_CARTEIRA2B@MMDadosCarteira@@2IB) already defined in HMApp.obj
>
> HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned
> int const MUDadosRecarga::_OFFSET_CARTEIRA2B"
> (?_OFFSET_CARTEIRA2B@MUDadosRecarga@@2IB) already defined in HMApp.obj
>
> PS: I didn't code "static unsigned int const". In my source code I coded
> "static const unsigned" only. I don't know why MSVC "translated" my code.
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> +++++++++++++++++++++++++++++++++++++++++++
> This Mail Was Scanned By Mail-seCure System
> at the Tel-Aviv University CC.
>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
@ 2004-06-28 22:46 Luciano Moreira - igLnx
2004-06-28 23:52 ` Glynn Clements
0 siblings, 1 reply; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-06-28 22:46 UTC (permalink / raw)
To: linux-c-programming
Is very commom in Java, the declaration of "static const" attributes
into class escope. Is possible to do anything similar in C++ ?
Using another techniques ?
All we need is to have a public static symbol into a class escope that
can be used to declare a array, intead of using a lot of global symbols.
Thanks,
Luciano
Glynn Clements wrote:
>Luciano Moreira - igLnx wrote:
>
>
>
>>Sorry for this rookie question, but I'm not a experienced C++ programmer
>>(I m a old C-ANSI programmer, not in C++)
>>
>>How can us create a kind of "static const" attribute into a class ? I
>>need to use it like as:
>>char myArray[MYCLASS::DEFSIZE];
>>
>>
>
>That won't work. Array sizes have to be constant expressions
>(expressions which contain only literal values).
>
>
>
>>Where "DEFSIZE" whould be a "static const" member (or another type) of
>>MYCLASS.
>>
>>What do I already tried ? Follow:
>>class MYCLASS {
>> public:
>> static const int DEFSIZE;
>>};
>>const int DEFSIZE = 100;
>>
>>But, when the file "MYCLASS.h" is included more than one file ".cpp",
>>the linker says that the member "MYCLASS::DEFZISE" "was already defined
>>in 'filename'". And don't have occurrences of compiler errors, only on
>>linking.
>>
>>
>
>Definitions belong in source (.cpp) files, not header files.
>
>
>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-28 22:46 Luciano Moreira - igLnx
@ 2004-06-28 23:52 ` Glynn Clements
2004-06-29 14:30 ` Luciano Moreira - igLnx
2004-06-29 14:32 ` Micha Feigin
0 siblings, 2 replies; 28+ messages in thread
From: Glynn Clements @ 2004-06-28 23:52 UTC (permalink / raw)
To: Luciano Moreira - igLnx; +Cc: linux-c-programming
Luciano Moreira - igLnx wrote:
> Is very commom in Java, the declaration of "static const" attributes
> into class escope. Is possible to do anything similar in C++ ?
> Using another techniques ?
>
> All we need is to have a public static symbol into a class escope that
> can be used to declare a array, intead of using a lot of global symbols.
It appears that you are allowed to initialise constant static members
within the declaration, so you can do this:
class Foo
{
public:
static const int DEFSIZE = 100;
};
class Bar
{
public:
char myArray[Foo::DEFSIZE];
};
This compiles with "g++ -ansi -pedantic", so it's probably standard
ISO C++.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-28 23:52 ` Glynn Clements
@ 2004-06-29 14:30 ` Luciano Moreira - igLnx
2004-06-29 17:26 ` Henry Margies
2004-06-29 17:39 ` Glynn Clements
2004-06-29 14:32 ` Micha Feigin
1 sibling, 2 replies; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-06-29 14:30 UTC (permalink / raw)
To: linux-c-programming
Of corse, it's was my first attempt. But, the compiler says that "pure
specifier can only be specified for functions". Thus the below code
seems to be incorrect.
class Foo
{
public:
static const int DEFSIZE = 100;
};
Luciano
Glynn Clements wrote:
Glynn Clements wrote:
>Luciano Moreira - igLnx wrote:
>
>
>
>>Is very commom in Java, the declaration of "static const" attributes
>>into class escope. Is possible to do anything similar in C++ ?
>>Using another techniques ?
>>
>>All we need is to have a public static symbol into a class escope that
>>can be used to declare a array, intead of using a lot of global symbols.
>>
>>
>
>It appears that you are allowed to initialise constant static members
>within the declaration, so you can do this:
>
> class Foo
> {
> public:
> static const int DEFSIZE = 100;
> };
>
> class Bar
> {
> public:
> char myArray[Foo::DEFSIZE];
> };
>
>This compiles with "g++ -ansi -pedantic", so it's probably standard
>ISO C++.
>
>
>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-29 14:30 ` Luciano Moreira - igLnx
@ 2004-06-29 17:26 ` Henry Margies
2004-06-29 19:10 ` Luciano Moreira - igLnx
2004-06-29 17:39 ` Glynn Clements
1 sibling, 1 reply; 28+ messages in thread
From: Henry Margies @ 2004-06-29 17:26 UTC (permalink / raw)
To: linux-c-programming
Just put the definition in the cpp file.
Header File:
class Foo
{
public:
static const int DEFSIZE;
};
in CPP-File:
const int Foo::DEFSIZE=100
Why does this not work for you?
Henry
--
Hi! I'm a .signature virus! Copy me into your
~/.signature to help me spread!
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-29 17:26 ` Henry Margies
@ 2004-06-29 19:10 ` Luciano Moreira - igLnx
2004-07-02 7:47 ` wwp
0 siblings, 1 reply; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-06-29 19:10 UTC (permalink / raw)
Cc: linux-c-programming
Because, the compiler doesn't know the value of the symbol DEFSIZE when
it is used for a array declaration, like as:
char myArray[MYCLASS::DEFSIZE];
Then, we have the conclusion that the value atribution should be in
header file (.h) - for that case. But, I tried the use of "enum", and
now it's working well.
Luciano
Henry Margies wrote:
>Just put the definition in the cpp file.
>
>Header File:
>class Foo
>{
> public:
> static const int DEFSIZE;
>};
>
>in CPP-File:
>
>const int Foo::DEFSIZE=100
>
>
>Why does this not work for you?
>
>
>Henry
>
>
>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-29 19:10 ` Luciano Moreira - igLnx
@ 2004-07-02 7:47 ` wwp
[not found] ` <40E56EFF.1030901@ig.com.br>
0 siblings, 1 reply; 28+ messages in thread
From: wwp @ 2004-07-02 7:47 UTC (permalink / raw)
To: Luciano Moreira - igLnx; +Cc: linux-c-programming
Hello Luciano,
On Tue, 29 Jun 2004 16:10:15 -0300 Luciano Moreira - igLnx <lucianolnx@ig.com.br> wrote:
> Because, the compiler doesn't know the value of the symbol DEFSIZE when
> it is used for a array declaration, like as:
> char myArray[MYCLASS::DEFSIZE];
>
> Then, we have the conclusion that the value atribution should be in
> header file (.h) - for that case. But, I tried the use of "enum", and
> now it's working well.
Of course #define'd values can be used in array range. Such value is not
dynamically interpreted, since the pre processor is performing substitution of
macros before the compiler gets the file.
Did you try to maintain you const int in the header file and to encapsulate it
like this:
[beginning of .h file]
#ifndef MY_CLASS_H
#define MY_CLASS_H
// .. here the previous content of your .h file
#endif
[EOF]
My 2 cts..
Regards,
> Henry Margies wrote:
>
> >Just put the definition in the cpp file.
> >
> >Header File:
> >class Foo
> >{
> > public:
> > static const int DEFSIZE;
> >};
> >
> >in CPP-File:
> >
> >const int Foo::DEFSIZE=100
> >
> >
> >Why does this not work for you?
> >
> >
> >Henry
> >
> >
> >
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
--
wwp
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-29 14:30 ` Luciano Moreira - igLnx
2004-06-29 17:26 ` Henry Margies
@ 2004-06-29 17:39 ` Glynn Clements
1 sibling, 0 replies; 28+ messages in thread
From: Glynn Clements @ 2004-06-29 17:39 UTC (permalink / raw)
To: Luciano Moreira - igLnx; +Cc: linux-c-programming
Luciano Moreira - igLnx wrote:
> Of corse, it's was my first attempt. But, the compiler says that "pure
> specifier can only be specified for functions". Thus the below code
> seems to be incorrect.
It works for me with both egcs-2.91.66 and gcc-3.0.3.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-28 23:52 ` Glynn Clements
2004-06-29 14:30 ` Luciano Moreira - igLnx
@ 2004-06-29 14:32 ` Micha Feigin
2004-06-29 17:43 ` Glynn Clements
2004-06-29 19:08 ` Luciano Moreira - igLnx
1 sibling, 2 replies; 28+ messages in thread
From: Micha Feigin @ 2004-06-29 14:32 UTC (permalink / raw)
To: linux-c-programming
On Tue, Jun 29, 2004 at 12:52:04AM +0100, Glynn Clements wrote:
>
> Luciano Moreira - igLnx wrote:
>
> > Is very commom in Java, the declaration of "static const" attributes
> > into class escope. Is possible to do anything similar in C++ ?
> > Using another techniques ?
> >
> > All we need is to have a public static symbol into a class escope that
> > can be used to declare a array, intead of using a lot of global symbols.
>
> It appears that you are allowed to initialise constant static members
> within the declaration, so you can do this:
>
> class Foo
> {
> public:
> static const int DEFSIZE = 100;
> };
>
> class Bar
> {
> public:
> char myArray[Foo::DEFSIZE];
> };
>
> This compiles with "g++ -ansi -pedantic", so it's probably standard
> ISO C++.
Does this also work when put in a header or only when put directly in
the cpp file?
>
> --
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> +++++++++++++++++++++++++++++++++++++++++++
> This Mail Was Scanned By Mail-seCure System
> at the Tel-Aviv University CC.
>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-06-29 14:32 ` Micha Feigin
@ 2004-06-29 17:43 ` Glynn Clements
2004-06-30 1:33 ` Micha Feigin
2004-06-29 19:08 ` Luciano Moreira - igLnx
1 sibling, 1 reply; 28+ messages in thread
From: Glynn Clements @ 2004-06-29 17:43 UTC (permalink / raw)
To: Micha Feigin; +Cc: linux-c-programming
Micha Feigin wrote:
> > It appears that you are allowed to initialise constant static members
> > within the declaration, so you can do this:
> > This compiles with "g++ -ansi -pedantic", so it's probably standard
> > ISO C++.
>
> Does this also work when put in a header or only when put directly in
> the cpp file?
It doesn't matter. A #include directive simply inserts the contents of
the header file directly into the source code which is fed to the
compiler. The compiler doesn't care which file a given piece of code
comes from.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-29 17:43 ` Glynn Clements
@ 2004-06-30 1:33 ` Micha Feigin
2004-06-30 2:01 ` Glynn Clements
2004-06-30 20:39 ` John Richard Moser
0 siblings, 2 replies; 28+ messages in thread
From: Micha Feigin @ 2004-06-30 1:33 UTC (permalink / raw)
To: linux-c-programming
On Tue, Jun 29, 2004 at 06:43:23PM +0100, Glynn Clements wrote:
>
> Micha Feigin wrote:
>
> > > It appears that you are allowed to initialise constant static members
> > > within the declaration, so you can do this:
>
> > > This compiles with "g++ -ansi -pedantic", so it's probably standard
> > > ISO C++.
> >
> > Does this also work when put in a header or only when put directly in
> > the cpp file?
>
> It doesn't matter. A #include directive simply inserts the contents of
> the header file directly into the source code which is fed to the
> compiler. The compiler doesn't care which file a given piece of code
> comes from.
>
It is a problem since a header can be included more then once, and
since the static value is put into the data section of the object, the
linker should give an error if the file was included more the once
since it will have multiple definitions for the same variable and it
wont know how to link the objects (it doesn't care if it the same
values, it does care if the static variable gets its value more then
once).
I had that problem with the arpack++ library which is implemented in
headers only and it sets such a variable and crashes miserably if I
split the code between files.
> --
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> +++++++++++++++++++++++++++++++++++++++++++
> This Mail Was Scanned By Mail-seCure System
> at the Tel-Aviv University CC.
>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 1:33 ` Micha Feigin
@ 2004-06-30 2:01 ` Glynn Clements
2004-06-30 23:42 ` Daniel Brockman
2004-06-30 20:39 ` John Richard Moser
1 sibling, 1 reply; 28+ messages in thread
From: Glynn Clements @ 2004-06-30 2:01 UTC (permalink / raw)
To: Micha Feigin; +Cc: linux-c-programming
Micha Feigin wrote:
> > > > It appears that you are allowed to initialise constant static members
> > > > within the declaration, so you can do this:
> >
> > > > This compiles with "g++ -ansi -pedantic", so it's probably standard
> > > > ISO C++.
> > >
> > > Does this also work when put in a header or only when put directly in
> > > the cpp file?
> >
> > It doesn't matter. A #include directive simply inserts the contents of
> > the header file directly into the source code which is fed to the
> > compiler. The compiler doesn't care which file a given piece of code
> > comes from.
>
> It is a problem since a header can be included more then once, and
> since the static value is put into the data section of the object, the
> linker should give an error if the file was included more the once
> since it will have multiple definitions for the same variable and it
> wont know how to link the objects (it doesn't care if it the same
> values, it does care if the static variable gets its value more then
> once).
But in this case, there isn't a variable. Foo::DEFSIZE is a
compile-time constant; it doesn't exist in memory. Compiling the code
in question and running "nm" on both the object files and the
resulting executable confirms this.
If it was treated as a constant lvalue (i.e. stored in the .rodata
section), you wouldn't be able to use it to specify the array
dimension.
I can't state for certain that this is defined behaviour, because I
don't have a copy of the ISO C++ standard. OTOH, it works with
"g++ -ansi -pedantic ...", and that's usually fairly rigorous about
generating errors for non-standard code.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 2:01 ` Glynn Clements
@ 2004-06-30 23:42 ` Daniel Brockman
2004-07-01 22:33 ` Glynn Clements
0 siblings, 1 reply; 28+ messages in thread
From: Daniel Brockman @ 2004-06-30 23:42 UTC (permalink / raw)
To: linux-c-programming
Glynn Clements <glynn.clements@virgin.net> writes:
> Micha Feigin wrote:
>
>> > > > It appears that you are allowed to initialise constant static
>> > > > members within the declaration
>
> [...]
>
> But in this case, there isn't a variable. Foo::DEFSIZE is a
> compile-time constant; it doesn't exist in memory.
So what happens if you try to take its address?
--
Daniel Brockman
drlion@deepwood.net
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 23:42 ` Daniel Brockman
@ 2004-07-01 22:33 ` Glynn Clements
0 siblings, 0 replies; 28+ messages in thread
From: Glynn Clements @ 2004-07-01 22:33 UTC (permalink / raw)
To: Daniel Brockman; +Cc: linux-c-programming
Daniel Brockman wrote:
> >> > > > It appears that you are allowed to initialise constant static
> >> > > > members within the declaration
> >
> > [...]
> >
> > But in this case, there isn't a variable. Foo::DEFSIZE is a
> > compile-time constant; it doesn't exist in memory.
>
> So what happens if you try to take its address?
You get a link error, unless you also add a definition in one of the
source files.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 1:33 ` Micha Feigin
2004-06-30 2:01 ` Glynn Clements
@ 2004-06-30 20:39 ` John Richard Moser
2004-06-30 22:31 ` Glynn Clements
1 sibling, 1 reply; 28+ messages in thread
From: John Richard Moser @ 2004-06-30 20:39 UTC (permalink / raw)
To: Micha Feigin; +Cc: linux-c-programming
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Micha Feigin wrote:
| On Tue, Jun 29, 2004 at 06:43:23PM +0100, Glynn Clements wrote:
|
|>It doesn't matter. A #include directive simply inserts the contents of
|>the header file directly into the source code which is fed to the
|>compiler. The compiler doesn't care which file a given piece of code
|>comes from.
|>
|
| It is a problem since a header can be included more then once, and
Umm.
#import "foo.h"
This includes only one time. I'd only use this with Objective-C though.
For C, and in general, you should use:
#ifndef __FOO_H__
#define __FOO_H__
....
#endif /*__FOO_H__*/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFA4yUShDd4aOud5P8RAkoxAJ9mYFKq+MYzPsX+LGbg/iTDA+UFqQCfYnMv
2Tn7bkzzlPu66jmrOamHJW8=
=nfHE
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 20:39 ` John Richard Moser
@ 2004-06-30 22:31 ` Glynn Clements
2004-07-01 3:22 ` Micha Feigin
2004-07-01 7:12 ` John Richard Moser
0 siblings, 2 replies; 28+ messages in thread
From: Glynn Clements @ 2004-06-30 22:31 UTC (permalink / raw)
To: John Richard Moser; +Cc: Micha Feigin, linux-c-programming
John Richard Moser wrote:
> |>It doesn't matter. A #include directive simply inserts the contents of
> |>the header file directly into the source code which is fed to the
> |>compiler. The compiler doesn't care which file a given piece of code
> |>comes from.
> |>
> |
> | It is a problem since a header can be included more then once, and
>
> Umm.
>
> #import "foo.h"
>
> This includes only one time. I'd only use this with Objective-C though.
>
> For C, and in general, you should use:
>
> #ifndef __FOO_H__
> #define __FOO_H__
>
> ....
>
> #endif /*__FOO_H__*/
That only prevents against the header being included multiple times
within a given compilation unit, which isn't the issue which was being
discussed. The issue was that, if an lvalue is defined (as opposed to
declared) in a header file, it will exist in every object file whose
source code included the header.
The key point is that C++ appears to treat initialised "static const"
members as compile-time constants, not as lvalues. Thus, they don't
exist in any object file, and so the problem of them existing in
multiple object files doesn't arise.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 22:31 ` Glynn Clements
@ 2004-07-01 3:22 ` Micha Feigin
2004-07-01 4:27 ` Glynn Clements
2004-07-01 7:12 ` John Richard Moser
1 sibling, 1 reply; 28+ messages in thread
From: Micha Feigin @ 2004-07-01 3:22 UTC (permalink / raw)
To: linux-c-programming
On Wed, Jun 30, 2004 at 11:31:49PM +0100, Glynn Clements wrote:
>
> John Richard Moser wrote:
>
> > |>It doesn't matter. A #include directive simply inserts the contents of
> > |>the header file directly into the source code which is fed to the
> > |>compiler. The compiler doesn't care which file a given piece of code
> > |>comes from.
> > |>
> > |
> > | It is a problem since a header can be included more then once, and
> >
> > Umm.
> >
> > #import "foo.h"
> >
> > This includes only one time. I'd only use this with Objective-C though.
> >
> > For C, and in general, you should use:
> >
> > #ifndef __FOO_H__
> > #define __FOO_H__
> >
> > ....
> >
> > #endif /*__FOO_H__*/
>
> That only prevents against the header being included multiple times
> within a given compilation unit, which isn't the issue which was being
> discussed. The issue was that, if an lvalue is defined (as opposed to
> declared) in a header file, it will exist in every object file whose
> source code included the header.
>
> The key point is that C++ appears to treat initialised "static const"
> members as compile-time constants, not as lvalues. Thus, they don't
> exist in any object file, and so the problem of them existing in
> multiple object files doesn't arise.
That can't be true; What happens if the static int value is defined in
another file? it's value won't be known at compile time, only at link
time. What g++ may be doing is ignoring the problem of multiple
definitions if they have the same value.
It does allow initializing array size using static const, unlike gcc, I
don't know the mechanism, but I believe it also requires linker
support, not only compiler.
I compiled a sample program with a static const int val = 10; and then
ran nm on it and in the output:
a.out:080484c4 r val
so the value is in the object, read only data if I recall the section
symbols correctly.
>
> --
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> +++++++++++++++++++++++++++++++++++++++++++
> This Mail Was Scanned By Mail-seCure System
> at the Tel-Aviv University CC.
>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-07-01 3:22 ` Micha Feigin
@ 2004-07-01 4:27 ` Glynn Clements
0 siblings, 0 replies; 28+ messages in thread
From: Glynn Clements @ 2004-07-01 4:27 UTC (permalink / raw)
To: Micha Feigin; +Cc: linux-c-programming
Micha Feigin wrote:
> > The key point is that C++ appears to treat initialised "static const"
> > members as compile-time constants, not as lvalues. Thus, they don't
> > exist in any object file, and so the problem of them existing in
> > multiple object files doesn't arise.
>
> That can't be true; What happens if the static int value is defined in
> another file? it's value won't be known at compile time, only at link
> time.
By "initialised", I'm referring to the situation where they are
initialised in the class declaration. See my previous example.
> What g++ may be doing is ignoring the problem of multiple
> definitions if they have the same value.
No.
The class declaration doesn't cause any lvalues to be created. There
are no definitions in the object file. Try it.
> It does allow initializing array size using static const, unlike gcc, I
> don't know the mechanism, but I believe it also requires linker
> support, not only compiler.
No.
In most situations, the size of an array has to be known at compile
time. If the array is a local variable, its size will affect the the
frame offsets of any local variables which are below it in the stack
frame. If the array is a structure field, its size will affect the
size of the structure. If you multiply by sizeof(array), the compiler
will often generate different code depending upon whether
sizeof(array) is a power of two. And so on.
The only exceptions are in declarations where you don't need to
specify the size, e.g.:
extern int array[];
and:
void foo(int array[]);
Anywhere that you have to specify a size, the size needs to be known
at compile time (variable-sized automatic arrays are permitted in C99,
but not in C89 or C++, although gcc will allow them unless you use
-pedantic; variable-sized global arrays and structure fields aren't
allowed anywhere and aren't supported by gcc).
> I compiled a sample program with a static const int val = 10; and then
> ran nm on it and in the output:
>
> a.out:080484c4 r val
>
> so the value is in the object, read only data if I recall the section
> symbols correctly.
That's a variable definition, not a class declaration.
Here's a concrete example:
$ cat foo.cc
#include <stdio.h>
class Foo
{
public:
static const int DEFSIZE = 100;
};
int main(void) {
printf("%d\n", Foo::DEFSIZE);
return 0;
}
$ g++ -c -ansi -pedantic -Wall foo.cc
$ nm foo.o
00000000 T main
U printf
$ g++ foo.o
$ ./a.out
100
If you try to use Foo::DEFSIZE as an lvalue (e.g. taking its address),
you will get a link error unless you actually add a definition for it.
But you can use it as a value without supplying a definition, and
nothing will appear in the object file. Also, if it's initialised in
the declaration, you can omit the initialiser from any subsequent
definition, i.e.:
$ cat foo.cc
#include <stdio.h>
class Foo
{
public:
static const int DEFSIZE = 100;
};
const int Foo::DEFSIZE;
const int *ptr = &Foo::DEFSIZE;
int main(void) {
printf("%d\n", *ptr);
return 0;
}
$ g++ -ansi -pedantic -Wall foo.cc
$ ./a.out
100
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-30 22:31 ` Glynn Clements
2004-07-01 3:22 ` Micha Feigin
@ 2004-07-01 7:12 ` John Richard Moser
1 sibling, 0 replies; 28+ messages in thread
From: John Richard Moser @ 2004-07-01 7:12 UTC (permalink / raw)
To: Glynn Clements; +Cc: Micha Feigin, linux-c-programming
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Glynn Clements wrote:
| John Richard Moser wrote:
|
|
|>|>It doesn't matter. A #include directive simply inserts the contents of
|>|>the header file directly into the source code which is fed to the
|>|>compiler. The compiler doesn't care which file a given piece of code
|>|>comes from.
|>|>
|>|
|>| It is a problem since a header can be included more then once, and
|>
|>Umm.
|>
|>#import "foo.h"
|>
|>This includes only one time. I'd only use this with Objective-C though.
|>
|>For C, and in general, you should use:
|>
|>#ifndef __FOO_H__
|>#define __FOO_H__
|>
|>....
|>
|>#endif /*__FOO_H__*/
|
|
| That only prevents against the header being included multiple times
| within a given compilation unit, which isn't the issue which was being
| discussed. The issue was that, if an lvalue is defined (as opposed to
| declared) in a header file, it will exist in every object file whose
| source code included the header.
|
yeah, externally linked rather than staticly linked. If you declare
them static, they won't be associated with an object symbol, thus it's
like they don't exist in relation to linking multiple objects together.
It goes like this:
int foo; /*We can say 'extern int foo' somewhere and access this from
another compilation unit*/
static int bar; /*This we can't.*/
| The key point is that C++ appears to treat initialised "static const"
| members as compile-time constants, not as lvalues. Thus, they don't
| exist in any object file, and so the problem of them existing in
| multiple object files doesn't arise.
Actually, they're the same as regular consts; they just don't get an
exported symbol, so they're invisible to the other compilation units.
They STILL get a position in the data segment!
|
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFA47lshDd4aOud5P8RAumVAJ9CmfPM1lUlOvDQjT3Dz8CG9ayg4QCZAekl
e5/IVRksVVq0HldmWYFDcv8=
=6vVS
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
2004-06-29 14:32 ` Micha Feigin
2004-06-29 17:43 ` Glynn Clements
@ 2004-06-29 19:08 ` Luciano Moreira - igLnx
1 sibling, 0 replies; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-06-29 19:08 UTC (permalink / raw)
Cc: linux-c-programming
Your previous suggestion is fine !!!
I tried to use "enum" and it's working well. Thus, it's a way to
encapsulate a constant into a class escope.
Of course that, it isn't the better way to encapsulate constants into a
class escope like the use of "static const" (like as in Java), but it's
working.
If someone have anothers tecniques, they ll be welcome.
Thank you all,
Luciano
Micha Feigin wrote:
>On Tue, Jun 29, 2004 at 12:52:04AM +0100, Glynn Clements wrote:
>
>
>>Luciano Moreira - igLnx wrote:
>>
>>
>>
>>>Is very commom in Java, the declaration of "static const" attributes
>>>into class escope. Is possible to do anything similar in C++ ?
>>>Using another techniques ?
>>>
>>>All we need is to have a public static symbol into a class escope that
>>>can be used to declare a array, intead of using a lot of global symbols.
>>>
>>>
>>It appears that you are allowed to initialise constant static members
>>within the declaration, so you can do this:
>>
>> class Foo
>> {
>> public:
>> static const int DEFSIZE = 100;
>> };
>>
>> class Bar
>> {
>> public:
>> char myArray[Foo::DEFSIZE];
>> };
>>
>>This compiles with "g++ -ansi -pedantic", so it's probably standard
>>ISO C++.
>>
>>
>
>Does this also work when put in a header or only when put directly in
>the cpp file?
>
>
>
>>--
>>Glynn Clements <glynn.clements@virgin.net>
>>-
>>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>>the body of a message to majordomo@vger.kernel.org
>>More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>> +++++++++++++++++++++++++++++++++++++++++++
>> This Mail Was Scanned By Mail-seCure System
>> at the Tel-Aviv University CC.
>>
>>
>>
>-
>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
>
^ permalink raw reply [flat|nested] 28+ messages in thread
* Re: "static const" attribute in C++ (How to ?)
@ 2004-07-02 10:20 Dinesh Ahuja
2004-07-02 14:29 ` Luciano Moreira - igLnx
2004-07-02 22:32 ` Glynn Clements
0 siblings, 2 replies; 28+ messages in thread
From: Dinesh Ahuja @ 2004-07-02 10:20 UTC (permalink / raw)
To: lucianolnx, subscript; +Cc: linux-c-programming
Hi Henry,
There is a concept of constant folding in C++.
Whenever you declare any data member of class as a
constant, the compiler should allocate the memory for
it in the object layout and compiler should know the
size of the variable in advance. Suppose, if I have a
class like below :
class MyClass {
const int size;
int arrSize[size];
};
This will not compile because compiler can not do the
constant folding in above case as memory needs to be
allocated for the constant data member size.
The above problem can be resolved by using enums.
class MyClass {
enum {size = 100};
int arrSize[size];
};
This will work as enum doesn't have any linkage and
hence no memory is allocated for them. Even use of
extern keyword prevents constant folding for a
constant variable.
I hope the above thing may have helped you.
Thanks & Regards
Dinesh-Ahuja
> > Henry Margies wrote:
> >Just put the definition in the cpp file.
> >Header File:
> >class Foo
> >{
> > public:
> > static const int DEFSIZE;
> >};
> >
> >in CPP-File:
> >
> >const int Foo::DEFSIZE=100
>>
> >Why does this not work for you?
> >
> >Henry
________________________________________________________________________
Yahoo! India Careers: Over 50,000 jobs online
Go to: http://yahoo.naukri.com/
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-07-02 10:20 Dinesh Ahuja
@ 2004-07-02 14:29 ` Luciano Moreira - igLnx
2004-07-02 22:32 ` Glynn Clements
1 sibling, 0 replies; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-07-02 14:29 UTC (permalink / raw)
Cc: linux-c-programming
Yes ! It was my solution.
Luciano
Dinesh Ahuja wrote:
>Hi Henry,
>
>There is a concept of constant folding in C++.
>Whenever you declare any data member of class as a
>constant, the compiler should allocate the memory for
>it in the object layout and compiler should know the
>size of the variable in advance. Suppose, if I have a
>class like below :
>class MyClass {
> const int size;
> int arrSize[size];
>};
>This will not compile because compiler can not do the
>constant folding in above case as memory needs to be
>allocated for the constant data member size.
>
>The above problem can be resolved by using enums.
>class MyClass {
> enum {size = 100};
> int arrSize[size];
>};
>
>This will work as enum doesn't have any linkage and
>hence no memory is allocated for them. Even use of
>extern keyword prevents constant folding for a
>constant variable.
>
>I hope the above thing may have helped you.
>
>Thanks & Regards
>Dinesh-Ahuja
>
>
>
>>>Henry Margies wrote:
>>>Just put the definition in the cpp file.
>>>Header File:
>>>class Foo
>>>{
>>> public:
>>> static const int DEFSIZE;
>>>};
>>>
>>>in CPP-File:
>>>
>>>const int Foo::DEFSIZE=100
>>>
>>>Why does this not work for you?
>>>
>>>Henry
>>>
>>>
>
>
>
>________________________________________________________________________
>Yahoo! India Careers: Over 50,000 jobs online
>Go to: http://yahoo.naukri.com/
>
>
>
>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-07-02 10:20 Dinesh Ahuja
2004-07-02 14:29 ` Luciano Moreira - igLnx
@ 2004-07-02 22:32 ` Glynn Clements
2004-07-03 0:12 ` Luciano Moreira - igLnx
1 sibling, 1 reply; 28+ messages in thread
From: Glynn Clements @ 2004-07-02 22:32 UTC (permalink / raw)
To: Dinesh Ahuja; +Cc: lucianolnx, linux-c-programming
Dinesh Ahuja wrote:
> There is a concept of constant folding in C++.
> Whenever you declare any data member of class as a
> constant, the compiler should allocate the memory for
> it in the object layout and compiler should know the
> size of the variable in advance. Suppose, if I have a
> class like below :
>
> class MyClass {
> const int size;
> int arrSize[size];
> };
> This will not compile because compiler can not do the
> constant folding in above case as memory needs to be
> allocated for the constant data member size.
However, the title explicitly says:
STATIC const attribute in C++ (How to ?)
[Emphasis mine.]
There is a difference.
> The above problem can be resolved by using enums.
> class MyClass {
> enum {size = 100};
> int arrSize[size];
> };
It can also be resolved using an initialised static const member:
class MyClass {
static const int size = 100;
int arrSize[size];
};
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-07-02 22:32 ` Glynn Clements
@ 2004-07-03 0:12 ` Luciano Moreira - igLnx
2004-07-05 5:35 ` Dinesh Ahuja
0 siblings, 1 reply; 28+ messages in thread
From: Luciano Moreira - igLnx @ 2004-07-03 0:12 UTC (permalink / raw)
To: Glynn Clements; +Cc: Dinesh Ahuja, linux-c-programming
Ok, but during our talk, I had asked for anothers ways to do anything
similar, and I get a way using enum. And another hands, my compiler
(MSVC), doesn't compile static const "initialized member", like as:
class MyClass {
static const int size = 100;
int arrSize[size];
};
It seems to think that I m trying to initialize a pure virutal function with wrog sintax, because it need "=0" and not "=100".
Sorry, I dont know why, but MSVC cannot compile it, I already searched MS documentation, but no answers.
Luciano
Glynn Clements wrote:
>Dinesh Ahuja wrote:
>
>
>
>>There is a concept of constant folding in C++.
>>Whenever you declare any data member of class as a
>>constant, the compiler should allocate the memory for
>>it in the object layout and compiler should know the
>>size of the variable in advance. Suppose, if I have a
>>class like below :
>>
>>class MyClass {
>> const int size;
>> int arrSize[size];
>>};
>>This will not compile because compiler can not do the
>>constant folding in above case as memory needs to be
>>allocated for the constant data member size.
>>
>>
>
>However, the title explicitly says:
>
> STATIC const attribute in C++ (How to ?)
>
>[Emphasis mine.]
>
>There is a difference.
>
>
>
>>The above problem can be resolved by using enums.
>>class MyClass {
>> enum {size = 100};
>> int arrSize[size];
>>};
>>
>>
>
>It can also be resolved using an initialised static const member:
>
>class MyClass {
> static const int size = 100;
> int arrSize[size];
>};
>
>
>
^ permalink raw reply [flat|nested] 28+ messages in thread* Re: "static const" attribute in C++ (How to ?)
2004-07-03 0:12 ` Luciano Moreira - igLnx
@ 2004-07-05 5:35 ` Dinesh Ahuja
0 siblings, 0 replies; 28+ messages in thread
From: Dinesh Ahuja @ 2004-07-05 5:35 UTC (permalink / raw)
To: Luciano Moreira - igLnx; +Cc: linux-c-programming
The problem which you are facing is a bug in MSVC.
MSVC does not support in-class initialization of const
static data member.
Read the following link :
http://www.corfield.org/index.php?event=cplusplus.section§ion=mcon
The compiler which supports in-class initialization of
const static data members provides the feature of
const-folding and hence it can be used to set the size
of an array.
Regards
Dinesh
--- Luciano Moreira - igLnx <lucianolnx@ig.com.br>
wrote: > Ok, but during our talk, I had asked for
anothers
> ways to do anything
> similar, and I get a way using enum. And another
> hands, my compiler
> (MSVC), doesn't compile static const "initialized
> member", like as:
>
> class MyClass {
> static const int size = 100;
> int arrSize[size];
> };
>
> It seems to think that I m trying to initialize a
> pure virutal function with wrog sintax, because it
> need "=0" and not "=100".
> Sorry, I dont know why, but MSVC cannot compile it,
> I already searched MS documentation, but no answers.
>
> Luciano
>
> Glynn Clements wrote:
>
> >Dinesh Ahuja wrote:
> >
> >
> >
> >>There is a concept of constant folding in C++.
> >>Whenever you declare any data member of class as a
> >>constant, the compiler should allocate the memory
> for
> >>it in the object layout and compiler should know
> the
> >>size of the variable in advance. Suppose, if I
> have a
> >>class like below :
> >>
> >>class MyClass {
> >> const int size;
> >> int arrSize[size];
> >>};
> >>This will not compile because compiler can not do
> the
> >>constant folding in above case as memory needs to
> be
> >>allocated for the constant data member size.
> >>
> >>
> >
> >However, the title explicitly says:
> >
> > STATIC const attribute in C++ (How to ?)
> >
> >[Emphasis mine.]
> >
> >There is a difference.
> >
> >
> >
> >>The above problem can be resolved by using enums.
> >>class MyClass {
> >> enum {size = 100};
> >> int arrSize[size];
> >>};
> >>
> >>
> >
> >It can also be resolved using an initialised static
> const member:
> >
> >class MyClass {
> > static const int size = 100;
> > int arrSize[size];
> >};
> >
> >
> >
________________________________________________________________________
Yahoo! India Careers: Over 50,000 jobs online
Go to: http://yahoo.naukri.com/
^ permalink raw reply [flat|nested] 28+ messages in thread
end of thread, other threads:[~2004-07-05 5:35 UTC | newest]
Thread overview: 28+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-06-28 21:42 "static const" attribute in C++ (How to ?) Luciano Moreira - igLnx
2004-06-28 21:52 ` Glynn Clements
2004-06-29 1:10 ` Micha Feigin
-- strict thread matches above, loose matches on Subject: below --
2004-06-28 22:46 Luciano Moreira - igLnx
2004-06-28 23:52 ` Glynn Clements
2004-06-29 14:30 ` Luciano Moreira - igLnx
2004-06-29 17:26 ` Henry Margies
2004-06-29 19:10 ` Luciano Moreira - igLnx
2004-07-02 7:47 ` wwp
[not found] ` <40E56EFF.1030901@ig.com.br>
2004-07-02 14:30 ` Luciano Moreira - igLnx
2004-06-29 17:39 ` Glynn Clements
2004-06-29 14:32 ` Micha Feigin
2004-06-29 17:43 ` Glynn Clements
2004-06-30 1:33 ` Micha Feigin
2004-06-30 2:01 ` Glynn Clements
2004-06-30 23:42 ` Daniel Brockman
2004-07-01 22:33 ` Glynn Clements
2004-06-30 20:39 ` John Richard Moser
2004-06-30 22:31 ` Glynn Clements
2004-07-01 3:22 ` Micha Feigin
2004-07-01 4:27 ` Glynn Clements
2004-07-01 7:12 ` John Richard Moser
2004-06-29 19:08 ` Luciano Moreira - igLnx
2004-07-02 10:20 Dinesh Ahuja
2004-07-02 14:29 ` Luciano Moreira - igLnx
2004-07-02 22:32 ` Glynn Clements
2004-07-03 0:12 ` Luciano Moreira - igLnx
2004-07-05 5:35 ` Dinesh Ahuja
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).