linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* 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-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
* "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

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 22:46 "static const" attribute in C++ (How to ?) 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
  -- strict thread matches above, loose matches on Subject: below --
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
2004-06-28 21:42 Luciano Moreira - igLnx
2004-06-28 21:52 ` Glynn Clements
2004-06-29  1:10 ` Micha Feigin

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