473,434 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Initialising a constant from a file

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
Jul 19 '05 #1
4 1967
On 7 Sep 2003 03:02:48 -0700, zo**@rivernet.com.au (Peter Kohout) wrote:
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"?

Jul 19 '05 #2
On 7 Sep 2003 06:00:36 -0700, zo**@rivernet.com.au (Peter Kohout) wrote:
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.

Jul 19 '05 #3
zo**@rivernet.com.au (Peter Kohout) wrote in message news:<dc**************************@posting.google. com>...
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
Jul 19 '05 #4
Peter Kohout wrote:
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

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mattias Brändström | last post by:
Hello all! I am trying to write code that allows me to initialise one of my classes inline (with a vector like structure). Inline might not be the best term to use here but I can't think of any...
2
by: Dylan | last post by:
what is the best way of initializing a static std::vector data member with some values? (currently I just push_back some values in the constructor if the size == 0) thanks
8
by: Nick Keighley | last post by:
Hi, is there a way of initialising a vector in a similar fashion to initialising an array? consider: Shape* triangle = new Triangle (); Shape* square = new Square (); Shape* shape_arr =...
9
by: Daisy | last post by:
Another this vs that post. Any real difference in declaring variables with default values, eg. public class MyControl : UserControl { public int RowHeight = 20; } Over just declaring the...
1
by: Serj Kondryukov | last post by:
I am novice in C#. I did write on C++ before. In my current program i need to use big constant table. Each string is a some struct what contain following fields: MyEnum f1; MyEnum f2; bool f3;...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
32
by: Simon L | last post by:
BOOL bMyarray; memset(bMyArray , 0xFF, sizeof(BOOL) * 1000); clean & quick until someone else's code found I was returning -1 to mean true Because my BOOL is 4 bytes long, TRUE in memory...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.