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

Static Initialization of Base Class' member

Hello there.
I have an abstract Base class with bunch of private static members and
public static getters for those members.

I want my Derived classes to call a static constructor and initialize
those members. How can this be achieved? Thank you.
Jul 25 '08 #1
4 2978
On Jul 25, 1:31*pm, dani...@mail.ru wrote:
Hello there.
I have an abstract Base class with bunch of private static members and
public static getters for those members.

I want my Derived classes to call a static constructor and initialize
those members. How can this be achieved? Thank you.
Use a protected static initializer function:

class Base
{
static int i;
protected:
static void Init( const int j ) { i = j; }
public:
static int GetI() { return i; }
};

int Base::i = 0;

class Derived : public Base
{
public:
Derived() { Init( 42 ); }
};

Cheers! --M
Jul 25 '08 #2
On Jul 25, 1:39*pm, mlimber <mlim...@gmail.comwrote:
On Jul 25, 1:31*pm, dani...@mail.ru wrote:
Hello there.
I have an abstract Base class with bunch of private static members and
public static getters for those members.
I want my Derived classes to call a static constructor and initialize
those members. How can this be achieved? Thank you.

Use a protected static initializer function:

*class Base
*{
* *static int i;
*protected:
* *static void Init( const int j ) { i = j; }
*public:
* *static int GetI() { return i; }
*};

*int Base::i = 0;

*class Derived : public Base
*{
*public:
* *Derived() { Init( 42 ); }
*};

Cheers! --M
Thank you for your quick response. The version that you gave me does
work, however, as you can see, the static Init() gets called every
time a new instance is being created. To me, it defeats the whole
purpose of the object being static in the first place. Is there a way
for me to call the static Init() only once? I tried to put
"Derived::Init()" but that didn't compile.
Jul 25 '08 #3
On Jul 25, 2:57*pm, dani...@mail.ru wrote:
On Jul 25, 1:39*pm, mlimber <mlim...@gmail.comwrote:
On Jul 25, 1:31*pm, dani...@mail.ru wrote:
Hello there.
I have an abstract Base class with bunch of private static members and
public static getters for those members.
I want my Derived classes to call a static constructor and initialize
those members. How can this be achieved? Thank you.
Use a protected static initializer function:
*class Base
*{
* *static int i;
*protected:
* *static void Init( const int j ) { i = j; }
*public:
* *static int GetI() { return i; }
*};
*int Base::i = 0;
*class Derived : public Base
*{
*public:
* *Derived() { Init( 42 ); }
*};
Cheers! --M

Thank you for your quick response. The version that you gave me does
work, however, as you can see, the static Init() gets called every
time a new instance is being created. To me, it defeats the whole
purpose of the object being static in the first place. Is there a way
for me to call the static Init() only once? I tried to put
"Derived::Init()" but that didn't compile.
Base::Init
{
static bool first = true;
if(first)
{
first = false;
//do your stuff WARNING NOT THREAD SAFE
}
}

this Init is called only once
Jul 25 '08 #4
da*****@mail.ru wrote in news:dae40744-516d-4e3f-b166-
de**********@x29g2000prd.googlegroups.com:
>
Thank you for your quick response. The version that you gave me does
work, however, as you can see, the static Init() gets called every
time a new instance is being created. To me, it defeats the whole
purpose of the object being static in the first place. Is there a way
for me to call the static Init() only once? I tried to put
"Derived::Init()" but that didn't compile.
If you require dynamic initialization, you can't help it being called, but
you can protect it with a boolean that gets set to true after the init()
call finishes. Like so:

class Whatever {
static bool initialized;
static void Init()
{
if (!initialized)
{
// init stuff
initialized = true;
}
}

};

bool Whatever::initialized = false;
You might also consider a singleton instead of statics.

joe
Jul 25 '08 #5

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

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
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...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
5
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
15
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.