473,503 Members | 3,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Complex Static Initializations

I would like to know the best way to initialize complex static member
variables. In addition, I want to avoid creating an Init() method
that is called by the ctor, since there's no need to wait for that
call in my application. My current attempt is OK, but it creates an
extra member variable that is extraneous:

<code>
class A {
private:
static std::vector<int> _v;
static bool init();
static bool isInit;
public:
A() : _a(1) {}
};

// This would go in the implementation file

std::vector<int> A::_v;
bool A::init() {
_v.push_back(1);
_v.push_back(2);
return true;
}
bool A::isInit = A::init();
</code>

Is there a better way to do this?

thanks for any thoughts,
cpp
Jul 22 '05 #1
1 1462
I'm not following your posting, but I would suggest you consider the
singleton pattern. Basically, this constructs a single static object and
provides a pointer to it. You can also control the construction so that if
you never access the object, it is never created. You can put just about
any initialization into the static object you wish.

Here's an example you can use....

#include <vector>
using namespace std;

class Singleton {
private:
static Singleton* _data;

std::vector<int> _v;
public:
Singleton()
{
_v.push_back(1);
_v.push_back(2);
}
int get_first()
{
return _v[0];
}
int get_second()
{
return _v[1];
}
static Singleton* getData()
{
if (_data ==0)
{
static Singleton s;
_data = &s;

}
return _data;

}
};
Singleton* Singleton::_data = 0;

int main(int argc, char* argv[])
{

Singleton* s = Singleton::getData();
int second = s->get_first();
int first = s->get_second();
return 0;
}

"cppaddict" <he***@hello.com> wrote in message
news:st********************************@4ax.com...
I would like to know the best way to initialize complex static member
variables. In addition, I want to avoid creating an Init() method
that is called by the ctor, since there's no need to wait for that
call in my application. My current attempt is OK, but it creates an
extra member variable that is extraneous:

<code>
class A {
private:
static std::vector<int> _v;
static bool init();
static bool isInit;
public:
A() : _a(1) {}
};

// This would go in the implementation file

std::vector<int> A::_v;
bool A::init() {
_v.push_back(1);
_v.push_back(2);
return true;
}
bool A::isInit = A::init();
</code>

Is there a better way to do this?

thanks for any thoughts,
cpp

Jul 22 '05 #2

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

Similar topics

1
1437
by: Harald Deischinger | last post by:
I am using a source file with the following style (I know it is not very beautiful but it is working): SRC1.cpp: static void vFoo() { // bla bla bla } static int iInit1(name, fun) { //...
5
1795
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
9
2474
by: vp | last post by:
Can I safely assume that all static variables are initialized as NULL or zero, depending on the types of the variables, no matter on which platform that app is compiled ? Thanks for your help, ...
5
1689
by: Coder Coder | last post by:
Hello, Is it possible in C# to have the following. public class Foo { static { } public Foo ()
6
3539
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
20
6066
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
3
2661
by: Alexander Hans | last post by:
Hi, what's the usual way in C++ to define a static constant class member? Consider the following example: class TestClass { public: static const double a = 12.5; static const double b = 2...
17
4393
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
15
7837
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7064
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
7261
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
7315
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
6974
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
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
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...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.