All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Dinesh Ahuja" <mdlinux7@yahoo.co.in>
To: Luciano Moreira - igLnx <lucianolnx@ig.com.br>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: "static const" attribute in C++ (How to ?)
Date: Mon, 5 Jul 2004 06:35:27 +0100 (BST)	[thread overview]
Message-ID: <20040705053527.45632.qmail@web8304.mail.in.yahoo.com> (raw)
In-Reply-To: <40E5F9F5.4080009@ig.com.br>

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&section=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/

  reply	other threads:[~2004-07-05  5:35 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-02 10:20 "static const" attribute in C++ (How to ?) 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 message]
  -- 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-06-28 21:42 Luciano Moreira - igLnx
2004-06-28 21:52 ` Glynn Clements
2004-06-29  1:10 ` Micha Feigin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040705053527.45632.qmail@web8304.mail.in.yahoo.com \
    --to=mdlinux7@yahoo.co.in \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=lucianolnx@ig.com.br \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.