All of lore.kernel.org
 help / color / mirror / Atom feed
From: Micha Feigin <michf@post.tau.ac.il>
To: linux-c-programming <linux-c-programming@vger.kernel.org>
Subject: Re: "static const" attribute in C++ (How to ?)
Date: Tue, 29 Jun 2004 04:10:39 +0300	[thread overview]
Message-ID: <20040629011039.GB6335@luna.mooo.com> (raw)
In-Reply-To: <40E090AF.6090300@ig.com.br>

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

  parent reply	other threads:[~2004-06-29  1:10 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [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-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

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=20040629011039.GB6335@luna.mooo.com \
    --to=michf@post.tau.ac.il \
    --cc=linux-c-programming@vger.kernel.org \
    /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.