Peter said:
Hi everyone.
I have a class
CFoo
{
private:
static int N = 50;
int array[N];
//lots more code
};
Is it possible to initialise this class by reading N from an external file?
Thanking in advance
Peter
You could always do this:
#include <iostream>
#include <fstream>
using std::ifstream;
class MSFoo /* [1] */
{
public:
MSFoo();
virtual ~MSFoo();
private:
static int elems_in_array;
int * array;
};
MSFoo ::
MSFoo()
{
ifstream my_file("my_file.txt");
if (!my_file)
{
throw MSFoo_Initialization_Exception();
}
inp >> elems_in_array;
if (!inp.good())
{
throw MSFoo_Initialization_Exception();
}
array = new int[elems_in_array];
std::fill(array, array + elems_in_array, 0);
}
MSFoo ::
~MSFoo()
{
delete [] array;
}
A better idea is to use the std::vector and let your
dynamic allocation worries behind.
[1] If you are going to use the MFC naming convention of
prefixing your classes with 'C', you might as well use
"MS", for Microsoft. There is absolutely no requirement
in _standard_ C++ that your classes start with 'C'.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book