|
Boost Users : |
From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2006-02-16 11:23:42
Perry Smith wrote:
>If this is not an appropriate list for this question, please let me
>know.
>
>
It is not, but I'm going to answer the question this once.
>The books tell me to prefer const to #defines -- but where do you put
>them to have the right scope and not take up space in the object?
>
>
For in-class constants, you need to make them static, too.
>What I'd like to have is:
>
>class register {
> void set_mask() { reg |= mask; }
>
>private:
> uint64_t reg;
>const uint64_t mask = 0x12345678;
>};
>
>
->
static const uint64_t mask = 0x12345678
Note that older compilers may not support this syntax.
>But the compiler will not let me do this and even if it did,the mask
>would take up physical space. I don't want mask (or reg) to be
>visable outside of the class and I want the compiler to use mask as a
>compile time constant that does not consume space after the compile.
>
>
As long as you don't use mask in a context where it must take up space
(such as taking its address), it won't.
For future reference, a better place for straight C++ questions is
Usenet's comp.lang.c++, or if you feel more comfortable with a browser
interface, bulletin boards such as http://cboard.cprogramming.com/ .
Sebastian Redl
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net