Initialising a constant from a file

Discussion in 'C++' started by Peter Kohout, Sep 7, 2003.

  1. Peter Kohout

    Peter Kohout Guest

    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
    Peter Kohout, Sep 7, 2003
    #1
    1. Advertising

  2. On 7 Sep 2003 03:02:48 -0700, (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"?
    Alf P. Steinbach, Sep 7, 2003
    #2
    1. Advertising

  3. On 7 Sep 2003 06:00:36 -0700, (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.
    Alf P. Steinbach, Sep 7, 2003
    #3
  4. (Peter Kohout) wrote in message news:<>...
    > 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
    Pavel Vozenilek, Sep 7, 2003
    #4
  5. 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
    Thomas Matthews, Sep 8, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,074
  2. Martin Magnusson
    Replies:
    2
    Views:
    492
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    544
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    332
    Keith Thompson
    Dec 14, 2006
  5. Fraser Ross

    Initialising a constant object

    Fraser Ross, Nov 28, 2008, in forum: C++
    Replies:
    2
    Views:
    311
    Fraser Ross
    Nov 29, 2008
Loading...

Share This Page