Re: How to initialize a static array in a class constructor?
On 6/21/2014 3:56 PM, fl wrote:
I have searched on line about this topic, but they are different from my problem
I feel.
I think class polyphase_upsample_fir_24_c is defined right now. The
compiler complaints that polyphase_upsample_fir_24_c::taps undefined if
I comment out
taps = 1.0;
If it includes taps=1.0, it says: error #138: expression must be a modifiable
lvalue
My question is how I initializes taps value, which would be a static const float
array as an FIR taps (I have simplified it to a single static const value for
debug purpose).
Could you help me on this problem?
Thanks,
I'll try. First off, static data (regardless of the type) cannot be
initialized "in a class constructor." Static data are defined (and,
optionally, initialized) at the namespace level ("file scope"), except
for static integral constants which are allowed to be initialized in the
class definition.
That said...
class filter{
cmplx_float_c temp;
static const float taps;
Speaking of the design, what's the role of 'taps'? What does it do?
Why does it have to be static const *here in this class*?
public:
cmplx_float_c *get_input_pointer ();
filter(){ };
filter(const float){
taps = 1.0;
Why are you trying to assign to 'taps' in a constructor? Do you realize
that every instance of this class, regardless of how many of 'filter'
objects you're going to create, will try to set this value to 1.0...
Yes, it's const, and it's likely not going to change, but what if you
later decide to change it and assign it some state mid-way through its
life, and then create another 'filter'? What will happen to the stored
state?
I am trying to get you to think about your design. Is your 'filter' a
singleton (there is only one of it exists while the program is running)?
If so, you don't need 'taps' as static, do you? If it's not a
singleton, then you shouldn't program it so every instance of it resets
the 'taps'. It needs to be done once.
};
};
cmplx_float_c *filter::get_input_pointer (){
return &temp;
}
class polyphase_upsample_fir_24_c
{
public:
static const int UPSAMPLE_RATE = 24;
static const int TAPS_PER_PHASE = 16;
static const int MAX_INPUT_SAMPLE_COUNT = 48;
private:
polyphase_upsample_fir_c<UPSAMPLE_RATE, TAPS_PER_PHASE,
MAX_INPUT_SAMPLE_COUNT> filter;
It's A BAD IDEA(tm) to name your data using the same name you used for
some type. Just FYI.
static const float taps;
Where is this 'taps' object defined/initialized? Who needs this? Whose
object is this? There seems to exist some confusion between this static
data 'taps' and one with the same name in 'filter' class.
Have you thought this through?
public:
polyphase_upsample_fir_24_c():
filter(taps)
{}
void process(int input_sample_count, cmplx_float_c* output);
inline cmplx_float_c* get_input_pointer() { return filter.get_input_pointer(); }
};
In general, static data members are initialized *outside* of the class,
in a translation unit:
------------------------------ someclass.h
class someclass
{
static const int s_array[99];
public:
someclass(float number);
};
------------------------------ someclass.cpp
#include "someclass.h"
const int someclass::s_array[99] = { 0,1,2,3,4,5 }; // here it is!
someclass::someclass(float number) // constructor
{
// do something with 'number'
}
-------------------------------------------
V
--
I do not respond to top-posted replies, please don't ask