I have a class whose objects should share an array of coefficients. Normally, I'd make the shared element(s) static, but...I initialize their value from reading a file. Any ideas how I can:
1) initialize this array
2) keep only one copy among all objects within the class
Just write a static function Initialize() and call it in main.
If you want to do this from the constructor, you'll need to know whether the array has already been initialized. Either have a static boolean "initialized" and check it in the ctor or use a dynamic array and check if the pointer is null. Though there is no need to bind initialization of static members to a ctor.
That's a clever approach, Jose, and I'm going to remember it. I think, though, that Hamsterman is probably right; I was trying to get too "clever" for my own good. I'll just include something in main() that calls the function a bunch of times.
Well, it turns out that I get a build-time error when I try to make member data static. Here's a snippet from the declaration:
1 2
private:
staticlong coeffI[NBR_CELLS];
I reference the static variable twice, as arguments to non-member functions.
The error message I get is:
Undefined symbols:
"DemodShaper::coeffI", referenced from:
__ZN11DemodShaper6coeffIE$non_lazy_ptr in DemodShaper.o
(maybe you meant: __ZN11DemodShaper6coeffIE$non_lazy_ptr)
ld: symbol(s) not found
It matters because the identifier "DemodShaper::coeffI" is only known inside DemodShaper. If you want direct access to the array from outside the class, the array needs to be public.
But, to the called routines, it's just a pointer to a long. I'm not trying to actually reference the private element, just read from and write to its address. Isn't that OK, or am I going brain-dead?
void PrintValue(constlong someArr[], int index)
{
//It is only here where you "just get an address". Not in the function calls.
cout << "I can access the array: " << someArr[index];
}
class SomeClass
{
private:
staticconstlong Arr[];
public:
void Foo();
};
const SomeClass::Arr[] = { 1, 2, 3, 4, 5 };
void SomeClass::Foo()
{
PrintValue(SomeClass::Arr, 3); //Correct: This is a method inside the class.
}
void OutsideFoo()
{
PrintValue(SomeClass::Arr, 2); // Error: An outside function such as OutsideFoo() cannot access SomeClass::Arr.
}
Maybe I wasn't clear above: the second example I posted is from within a member function. Accordingly, it has access to the private data. Even though it's passing it to a non-member function, shouldn't it still be OK?
I see. But since the arrays are private, so should its length, I would think. Make NBR_CELLS a private member of DemodShaper? Just a suggestion. Whatever works for you.
So, to summarize...if I remove the static qualifier from the class definition, everything is OK. But, when I add "static" I get the build error mentioned above. Anyone have an idea about what's going on?
EDIT:
Found it! I'd forgotten to define the arrays outside of the class. (I'd forgotten about that aspect of static members.)