Initialization of static objects

Discussion in 'C++' started by junger, Mar 5, 2005.

  1. junger

    junger Guest

    If a member object is static, is it possible to initialize it with
    function calls? E.g., if I want my class to contain a static vector
    filled with other static objects, can I fill this vector statically
    without having tyo make a function call? Right now I'm using an array
    because I can specify an array's exact contents statically, but it
    seems stupid to have to use an array.

    Thank you.
    junger, Mar 5, 2005
    #1
    1. Advertising

  2. <junger> wrote...
    > If a member object is static, is it possible to initialize it with
    > function calls?


    Of course. Such initialisation is called "dynamic" and is done before
    the 'main' function is called.

    > E.g., if I want my class to contain a static vector
    > filled with other static objects, can I fill this vector statically
    > without having tyo make a function call? Right now I'm using an array
    > because I can specify an array's exact contents statically, but it
    > seems stupid to have to use an array.


    I am not sure I understand. Care to post some code?
    Victor Bazarov, Mar 6, 2005
    #2
    1. Advertising

  3. junger

    Rolf Magnus Guest

    junger <> wrote:

    > If a member object is static, is it possible to initialize it with
    > function calls?


    Yes.

    > E.g., if I want my class to contain a static vector
    > filled with other static objects, can I fill this vector statically
    > without having tyo make a function call?


    Huh? Above you wrote that you want to make a function call.
    Rolf Magnus, Mar 6, 2005
    #3
  4. junger

    junger Guest

    Yeah, sorry guys...I don't know wtf I was thikning when I wrote that
    question.

    The only two languages I know anything about are C++ and Java. I know
    what I want to do is possible in Java, and I'm hoping it is in C++.

    In Java, I can say:

    class Blah {

    private static ArrayList arrayList;

    static {
    arrayList = new ArrayList();
    arrayList.add("aaa");
    arrayList.add("bbb");
    arrayList.add("sakdljflaks");
    ... (etc.)
    }
    }


    Can I do this type of initialization of static data in C++? I was
    just saying I can do this with an array because I can specify its
    contents at construction-time, but not so with a vector. I don't want
    to have to clutter my code with "initialize()" function calls which
    execute code like that in the above "static" block.

    Thank you.



    On Sat, 05 Mar 2005 14:08:28 -0800, junger <> wrote:

    >If a member object is static, is it possible to initialize it with
    >function calls? E.g., if I want my class to contain a static vector
    >filled with other static objects, can I fill this vector statically
    >without having tyo make a function call? Right now I'm using an array
    >because I can specify an array's exact contents statically, but it
    >seems stupid to have to use an array.
    >
    >Thank you.
    junger, Mar 8, 2005
    #4
  5. junger wrote:
    >
    > Yeah, sorry guys...I don't know wtf I was thikning when I wrote that
    > question.
    >
    > The only two languages I know anything about are C++ and Java. I know
    > what I want to do is possible in Java, and I'm hoping it is in C++.
    >
    > In Java, I can say:
    >
    > class Blah {
    >
    > private static ArrayList arrayList;
    >
    > static {
    > arrayList = new ArrayList();
    > arrayList.add("aaa");
    > arrayList.add("bbb");
    > arrayList.add("sakdljflaks");
    > ... (etc.)
    > }
    > }
    >
    > Can I do this type of initialization of static data in C++? I was
    > just saying I can do this with an array because I can specify its
    > contents at construction-time, but not so with a vector. I don't want
    > to have to clutter my code with "initialize()" function calls which
    > execute code like that in the above "static" block.
    >


    If all else fails, one additional class often solves the problem
    (I don't know what an ArrayList is in Java, so I substitute with
    a vector of strings:)

    class StaticVector
    {
    public:
    StaticVector()
    {
    Data.push_back( "aaa" );
    Data.push_back( "bbb" );
    Data.push_back( "sakdljflaks" );
    ... (etc.)
    }

    std::vector< std::string > Data;
    };

    class Blah
    {
    private:
    static StaticVector arrayList;
    };

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Mar 8, 2005
    #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. JKop
    Replies:
    10
    Views:
    926
  2. benben
    Replies:
    1
    Views:
    288
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=
    Jul 18, 2005
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    431
    Victor Bazarov
    Jul 18, 2005
  4. Replies:
    6
    Views:
    451
    Ron Natalie
    Dec 11, 2005
  5. Mark P
    Replies:
    2
    Views:
    497
    Frederick Gotham
    Aug 11, 2006
Loading...

Share This Page