Initialising a constant from a file

P

Peter Kohout

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
 
A

Alf P. Steinbach

I have a class

CFoo
{
private:
static int N = 50;
int array[N];
//lots more code
};

The above does not compile, so you don't have it.

Post the smallest complete example that exhibits the problem.

Is it possible to initialise this class by reading N from an external file?

When do you want that to happen, and what do you mean by "initialise this
class"?
 
A

Alf P. Steinbach

Sorry for not making myself any clearer.
I would like to set the value of N in an external file, so that I can
vary the value of N without having to recompile the program.

I think the easiest thing to do then is to have N as a program argument
(simple in std. c++) or fetch it from an environment variable (depends
somewhat on the compiler).

The problem again is _when_ do you need to have N set.

If that is not critical, try something like


#include <cstdlib> // std::atoi

class Foo
{
protected:
friend int main( int nArgs, char* args[] );
static int N;
public:
Foo(){}
};

int Foo::N;


int main( int nArgs, char* args[] )
{
Foo::N = std::atoi( args[1] );

// Code that uses class Foo.
};


Hth.
 
P

Pavel Vozenilek

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?
If you like to read N every time application is started then you need
to add code to read it from file and use e.g. vector instead of array.

If you mean to use value from file during compile time then the only
way is ugly workaround:

static int N =
#include "my_file_with_number"
;


Metacode from Daveed Vanderwoorde may be able to do I/O during compile
time, hopefully, sometime in the future.

/Pavel
 
T

Thomas Matthews

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top