Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Static function initialization

80 views
Skip to first unread message

Reid Sweatman

unread,
Oct 7, 1998, 3:00:00 AM10/7/98
to
Maybe I'm missing something terribly obvious here, but how do you go
about initializing a static member object when the initializer is a
function, rather than a simple assignment? It can't be part of the
class constructor, but I can't think of any reasonable syntax for
specifying it at file scope.

The specific example that brought this up is the critical section
structure, which has to be global (I'm assuming it can be a static
class member, so long as every thread that has to access it can see
it). You have to call InitializeCriticalSection(), and I can't find
an example of how to do that at file scope.

rvc05

unread,
Oct 7, 1998, 3:00:00 AM10/7/98
to
Reid Sweatman wrote:

> Maybe I'm missing something terribly obvious here, but how do you go
> about initializing a static member object when the initializer is a
> function, rather than a simple assignment?

If I understand you right, you want to initialize a static member
object. If you use a pointer instead, you can write :

myClass {
static anyClass *member;
}

anyClass *myClass::member = new anyClass(someFunctionOrAnother());

Of course, you will need an anyClass constructor that accepts the return
type from someFunctionOrAnother().

Hope this helps (or even works, I haven't tested it in MSVC++).
G.


Don Grasberger

unread,
Oct 7, 1998, 3:00:00 AM10/7/98
to
Well, you could use lazy initialization, and have the object's
constructor initialize the object the first time it's used. i.e.

CBar
{
private:
static bool bInitialized ;
static CMyObject myObject ;
public:
CBar() ;
} ;

///...

bool CBar::bInitialized = false ;
CMyObject CBar::myObject ;

CBar::CBar()
{
if ( !bInitialized )
{
myObject->Initialize() ;
bInitialized = tru ;
}
} ;


On Wed, 07 Oct 1998 04:16:00 GMT, Reid.S...@m.cc.utah.edu (Reid
Sweatman) wrote:

>Maybe I'm missing something terribly obvious here, but how do you go
>about initializing a static member object when the initializer is a

>function, rather than a simple assignment? It can't be part of the
>class constructor, but I can't think of any reasonable syntax for
>specifying it at file scope.
>
>The specific example that brought this up is the critical section
>structure, which has to be global (I'm assuming it can be a static
>class member, so long as every thread that has to access it can see
>it). You have to call InitializeCriticalSection(), and I can't find
>an example of how to do that at file scope.
>


Don Grasberger
(remove --- from address to e-mail)

Andy Yee

unread,
Oct 7, 1998, 3:00:00 AM10/7/98
to
In article <361ae9b0...@msnews.microsoft.com>, Reid.S...@m.cc.utah.edu (Reid Sweatman) wrote:
>Maybe I'm missing something terribly obvious here, but how do you go
>about initializing a static member object when the initializer is a
>function, rather than a simple assignment? It can't be part of the
>class constructor, but I can't think of any reasonable syntax for
>specifying it at file scope.
>
>The specific example that brought this up is the critical section
>structure, which has to be global (I'm assuming it can be a static
>class member, so long as every thread that has to access it can see
>it). You have to call InitializeCriticalSection(), and I can't find
>an example of how to do that at file scope.
>

Can you post a code example or go into more details?

--------------------------------------------------------------------------------
Andy Yee Corporate E-mail: an...@xiotech.com
Principal Software Engineer Corporate Web Page: http://www.xiotech.com
XIOtech Corporation Personal E-mail: n...@yuck.net
Eden Prairie, MN Personal Home Page: http://www.visi.com/~nde

"Question authority...and the authorities will end up questioning YOU!"
--------------------------------------------------------------------------------

Aaron J Margosis

unread,
Oct 7, 1998, 3:00:00 AM10/7/98
to
Since there's no way (AFAIK) to tell whether a CRITICAL_SECTION has been
initialized, you'd probably benefit from an additional static bool and a static
wrapper function:

class MyClass
{
private:
static CRITICAL_SECTION m_critSec ;
static bool m_csInit ;
static CRITICAL_SECTION * GetCritSec() ;
[...]
};

Implemented ...

CRITICAL_SECTION MyClass::m_critSec ;
bool MyClass::m_csInit = false ;

CRITICAL_SECTION * MyClass::GetCritSec()
{
if ( ! m_csInit )
{
InitializeCriticalSection( &m_critSec ) ;
m_csInit = true ;
}
return &m_critSec ;
}


Used like this

... MyClass::MyMemberFunction()
{
...
EnterCriticalSection( GetCritSec() ) ;
... do stuff ...
LeaveCriticalSection( GetCritSec() ) ;
...
}

so that whenever you call GetCritSec() you're guaranteed an initialized CS. The
only remaining issue is to DeleteCriticalSection() when it will no longer be
used. Left as an exercise ...

HTH


Reid Sweatman wrote:
>
> Maybe I'm missing something terribly obvious here, but how do you go
> about initializing a static member object when the initializer is a
> function, rather than a simple assignment? It can't be part of the
> class constructor, but I can't think of any reasonable syntax for
> specifying it at file scope.
>
> The specific example that brought this up is the critical section
> structure, which has to be global (I'm assuming it can be a static
> class member, so long as every thread that has to access it can see
> it). You have to call InitializeCriticalSection(), and I can't find
> an example of how to do that at file scope.

-- Aaron [MVP]

---------------------------------
Aaron J Margosis
LCC International
Work e-mail: marg...@lcc.com
Work phone: 703-873-2622 (703-USE-A-MAC ??!!)
Personal: aaro...@erols.com

Doug Harrison

unread,
Oct 7, 1998, 3:00:00 AM10/7/98
to
On Wed, 07 Oct 1998 04:16:00 GMT, Reid.S...@m.cc.utah.edu (Reid
Sweatman) wrote:

>Maybe I'm missing something terribly obvious here, but how do you go
>about initializing a static member object when the initializer is a
>function, rather than a simple assignment?

Although initialization can use the = symbol, it is not assignment,
and an initializer can be a function.

>It can't be part of the
>class constructor, but I can't think of any reasonable syntax for
>specifying it at file scope.

You're asking how to initialize an object by calling a function
outside an initializer. The only way to do that is inside a function,
usually a ctor.

>The specific example that brought this up is the critical section
>structure, which has to be global (I'm assuming it can be a static
>class member, so long as every thread that has to access it can see
>it). You have to call InitializeCriticalSection(), and I can't find
>an example of how to do that at file scope.

You can wrap the CRITICAL_SECTION in a class, as sketched below:

class CritSec
{
public:

class Lock
{
public:

Lock(CritSec& cs) : m_cs(cs) { m_cs.Wait(); }
~Lock() { m_cs.Release(); }

private: // Data section.

CritSec& m_cs;
};

CritSec() { InitializeCriticalSection(&m_cs); }
~CritSec() { DeleteCriticalSection(&m_cs); }

void Wait() { EnterCriticalSection(&m_cs); }
void Release() { LeaveCriticalSection(&m_cs); }

private:

// Copyguard.
CritSec(const CritSec&);
void operator=(const CritSec&);

private: // Data section.

CRITICAL_SECTION m_cs;
};

Note that MFC wraps the Win32 synchronization objects and critical
section, but IMO, its approach is deeply flawed. From what I recall,
it pretends that critical sections are fundamentally similar to
mutexes and other sync objects represented by HANDLEs, and that is not
the case, so its multilock class asserts that the objects aren't
critical sections passed in through pointers to the common base class.
In short, what should be a compile-time error becomes a run-time
error. Also, the event wrapper contains members Lock and Unlock, which
are odd names that just don't belong there, because you don't lock
events. Also, CSyncObject::Lock returns a BOOL to represent a possibly
3-valued return, which isn't right. Also, CSingleLock's ctor takes a
pointer parameter, with the admonition that it cannot be 0, which
means it should be a reference, and its default behavior is not to
lock. Etc. Etc.

--
Doug Harrison
dHar...@worldnet.att.net

piy...@my-dejanews.com

unread,
Oct 8, 1998, 3:00:00 AM10/8/98
to
I'm afraid there is no clean way to do this.

One solution is to declare an initializaer class which initializes
all such variables.
In the constructor of this class call your initialization functions.
Declare a single static intance of this class in your source files.
So when this class gets initialized it initializes other static variables

here is the sample.

class GlobalInitializer {
GlobalInitializer ()
{
InitializeCriticalSection (&myCriticalSection);
}
};

Note that you have to take care of static order fiasco ( case where
GlobalInitializer gets constructed before myCriticalSection.

The way i would do it is this

CCricticalSection & myCriticalSection ()
{
static CCriticalSection *p = new CCriticalSection;
return *p;
}

class GlobalInitializer {
GlobalInitializer ()
{
InitializeCriticalSection (&myCriticalSection());
}
};

This guranties that myCriticalSection is initialized when
the constructor of GlobalInitializer is called.


Hope this helps.

Piyush


In article <361ae9b0...@msnews.microsoft.com>,


Reid.S...@m.cc.utah.edu (Reid Sweatman) wrote:
> Maybe I'm missing something terribly obvious here, but how do you go
> about initializing a static member object when the initializer is a

> function, rather than a simple assignment? It can't be part of the


> class constructor, but I can't think of any reasonable syntax for
> specifying it at file scope.
>

> The specific example that brought this up is the critical section
> structure, which has to be global (I'm assuming it can be a static
> class member, so long as every thread that has to access it can see
> it). You have to call InitializeCriticalSection(), and I can't find
> an example of how to do that at file scope.
>
>

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Reid Sweatman

unread,
Oct 8, 1998, 3:00:00 AM10/8/98
to

Yeah, this is what I'm after, but I still see a problem, in that
m_critSec doesn't get initialized at file scope, but rather in the
member function GetCritSec(). Unless there's a way to guarantee it
gets called at file scope before the class is instantiated, it looks
like it violates the requirement that static member data must be
initialized at file scope. I would assume that would apply to a
critical section structure declared as a static member object. No?

Reid Sweatman

unread,
Oct 8, 1998, 3:00:00 AM10/8/98
to
andyy@X-I-O-T-E-C-H-.C-O-M (Andy Yee) wrote:

>Can you post a code example or go into more details?

Okay. I need to start a new thread within a class member function.
In order to synchronize what that thread does with attempts by other
member functions to access data the thread is changing, I want to use
critical sections (seemed simplest). For that, I need a critical
section structure, which has to be visible to both the thread running
the class and the thread started in the member function. I'm guessing
I can make the structure a static member object, but you have to
initialize that kind of thing at file scope. Since this is done via
the InitializeCriticalSection() functions, which take a pointer to the
struct, I don't know what kind of syntax would be legal to do the
dirty deed.

Jason Smith

unread,
Oct 8, 1998, 3:00:00 AM10/8/98
to
Reid Sweatman wrote in message <361ae9b0...@msnews.microsoft.com>...
=Maybe I'm missing something terribly obvious here, but how do you go
=about initializing a static member object when the initializer is a
=function, rather than a simple assignment? It can't be part of the
=class constructor, but I can't think of any reasonable syntax for
=specifying it at file scope.

You create an object that takes care of it for you:

class CriticalSection
: public _CRITICAL_SECTION
{
public:
CriticalSection( void )
{ InitializeCriticalSection(this); }
~CriticalSection( void )
{ DeleteCriticalSection(this); }

// Other members as necessary.

};

Now define your static member as a CriticalSection (instead of the Win32 CS
type), and define in one location just as you would any other static:

CriticalSection MyClass::m_cs; // Calls default constructor

Jason.

--
Hi, I'm Bob. I'm a tomato. I'm here to help you.
- Bob


Aaron J Margosis

unread,
Oct 9, 1998, 3:00:00 AM10/9/98
to
Reid Sweatman wrote:
>
> Aaron J Margosis <marg...@lcc.com> wrote:
> >[...]

>
> Yeah, this is what I'm after, but I still see a problem, in that
> m_critSec doesn't get initialized at file scope, but rather in the
> member function GetCritSec(). Unless there's a way to guarantee it
> gets called at file scope before the class is instantiated, it looks
> like it violates the requirement that static member data must be
> initialized at file scope. I would assume that would apply to a
> critical section structure declared as a static member object. No?

No. CRITICAL_SECTION is just an ordinary structure which gets initialized on
construction like any other. You have to call InitializeCriticalSection()
before you can _use_ the structure for synchronization, but calling
InitializeCriticalSection() has nothing to do with C++ structure initialization.

BTW, don't miss Jason Smith's post in this thread.

HTH

-- Aaron [MVP]
---------------------
Aaron J Margosis
aaro...@erols.com
Fortress-NT - NT Workstation/Server Security Utility:
http://www.sunbelt-software.com/fortress.htm
ScrnSaveSwitch/Plus - Screen Saver Control Utility:
http://www.ssswitch.com

0 new messages