473,884 Members | 2,427 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 1484
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::_dat a = 0;

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

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

"cppaddict" <he***@hello.co m> wrote in message
news:st******** *************** *********@4ax.c om...
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
1482
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) { // access some global variables to register fun as name.
5
1816
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
2509
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, DT
5
1714
by: Coder Coder | last post by:
Hello, Is it possible in C# to have the following. public class Foo { static { } public Foo ()
6
3578
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 uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
20
6117
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.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
3
2678
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 * a;
17
4468
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 to 0. ANSI C guarantees that. Technically speaking, static variables go into the .bss ELF segment, while "static int foo=0" goes into .data. Because .bss is zero filled by the OS, it does not need to be in the actual binary. So it is in fact...
15
7884
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
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10769
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10869
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9591
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7985
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.