Initialization of static objects

J

junger

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.
 
V

Victor Bazarov

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?
 
R

Rolf Magnus

junger said:
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.
 
J

junger

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.
 
K

Karl Heinz Buchegger

junger said:
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;
};
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top