template<int fixp>
class TFixPoint {
private:
const int fixp_one = 1 << fixp;
const int fixp_half = 1 << (fixp - 1);
int fix_value;
...
...
...
};
const class members?
I wanna make a class dealing with fixed point numbers.
I also wanna handle the precision through the "fixp" template
parameter but I cannot declare my consts derived from "fixp", and I receive the following error message:
error C2258: illegal pure syntax, must be ''= 0''
Does it mean that I have to define const inline "fixp_one" and "fixp_half" member functions or I have to #define them?
Not nice at all! It must be a way to make a const local to a class but I don''t know how to do it.
Don''t initialise in the class definition, do it in the constructor in the initialiser section:
class WithAConst
{
public:
WithAConst();
private:
const int a;
};
WithAConst::WithAConst()
: a(0)
{
}
You can initialise the constant to different values with different constructors, if you like.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
class WithAConst
{
public:
WithAConst();
private:
const int a;
};
WithAConst::WithAConst()
: a(0)
{
}
You can initialise the constant to different values with different constructors, if you like.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
First off then you are speaking about member variables, not member functions. Second, you can''t initialise member variables like that - what you want is something like this:
Making the variables static means they are the same for all versions of FixPoint<37>, for instance. But do note that far from all compilers want to accept above. Hence, you may use the enum workaround for that, which would be something like...
Both of the above are used in the same way. I.e.
--
I am not a church numeral, I''''m a free variable!
template<int fixp>class FixPoint { static const int fixp_one = 1 << fixp; static const int fixp_half = 1 << (fixp-1); int fix_value;};
Making the variables static means they are the same for all versions of FixPoint<37>, for instance. But do note that far from all compilers want to accept above. Hence, you may use the enum workaround for that, which would be something like...
template<int fixp>class FixPoint { enum { fixp_one = 1 << fixp, fixp_half = 1 << (fixp-1) }; int fix_value;};
Both of the above are used in the same way. I.e.
Fixpoint<17>::fixp;
but I''ll let you experiment with that on your own. Hope it helps. --
I am not a church numeral, I''''m a free variable!
While we are talking about statics: static const variables must be initialized outside of the class definition. Just put a
static const int FixPoint::fixp_one = 1 << fixp;
in your implementation.
static const int FixPoint::fixp_one = 1 << fixp;
in your implementation.
quote:Original post by Shadowdancer
While we are talking about statics: static const variables must be initialized outside of the class definition. Just put a
static const int FixPoint::fixp_one = 1 << fixp;
in your implementation.
No they must not. Defining them as I did above is completely valid according to ISO/IEC 14882(1998) and works just fine in any decent compiler.
--
I am not a church numeral, I''m a free variable!
[edited by - muer on January 28, 2003 8:58:59 AM]
MSVC6 doesn't like static members initialised like someone mentioned (it is in the ISO standard though so slaps MS's wrist).
Using the initialiser list for a statically allocated (or global) object IS the same as using a real const. The items in the initialiser list are pre-initialised at compilation time and stuffed into the programs data section. That's why you should always use initialiser lists if possible. It has to initialise a dynamic object at run-time, but that's true for all dynamic consts.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
[edited by - Paradigm Shifter on January 28, 2003 9:04:39 AM]
Using the initialiser list for a statically allocated (or global) object IS the same as using a real const. The items in the initialiser list are pre-initialised at compilation time and stuffed into the programs data section. That's why you should always use initialiser lists if possible. It has to initialise a dynamic object at run-time, but that's true for all dynamic consts.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
[edited by - Paradigm Shifter on January 28, 2003 9:04:39 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement