473,406 Members | 2,217 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,406 software developers and data experts.

Initialization of static objects

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.
Jul 23 '05 #1
4 3308
<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?
Jul 23 '05 #2
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.

Jul 23 '05 #3
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.


Jul 23 '05 #4
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
kb******@gascad.at
Jul 23 '05 #5

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

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
5
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
14
by: seema | last post by:
Hi all, I am new to C++ programming. Can some body explain how to intialize the structure values, consider this program, #include <iostream.h> struct mystruct { int a,b,c,d,e,f; };
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
5
by: kiryazev | last post by:
Hello. Given the code below does C++ Standard guarantee that the function my_init() will be called before main()? struct A { A() { my_init(); } };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.