Non-boost c++ novice help

If this is not an appropriate list for this question, please let me know. 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? What I'd like to have is: class register { void set_mask() { reg |= mask; } private: uint64_t reg; const uint64_t mask = 0x12345678; }; 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. I vaguely know about namespace but if I put this inside a namespace, then I have to do: namespace hardware { const mask = 0x12345678; class register { void set_mask() { reg |= mask; } private: uint64_t reg; }; }; // .. define a register hardware::register doddy; and "mask" is not really private -- just obscured. I can still do: int i = hardware::mask; This just feels like I've taken a wrong turn somewhere. How would this be "properly" done? Thanks, Perry

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

You can only have static members defined in the class declaration, e.g. class register { void set_mask() { reg |= mask; } private: uint64_t reg; const static uint64_t mask = 0x12345678; }; This, of course, only makes sense when the mask doesn't change from every instantiation of register (being non-static). Christian
participants (3)
-
Christian Henning
-
Perry Smith
-
Sebastian Redl