Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Beginner
May 2, 1997



Application Security Testing: An Integral Part of DevOps

Constructor with static members in library

Background: Digital UNIX 3.2g, Alpha platform, latest GNU.

From the FAQ, I know that you are supposed to declare static member variables externally from the declaration and in only one place. But our group has run into a problem with that.

The code below works fine when it's just a couple of normal files compiled together. But what to do when that class is part of a library? Defining it in the related source causes us core dumps, which we traced to the fact that the static member class constructors did not run when placed in the library. If the static member class variables are defined in the main program, everything works fine again, but it just plain feels wrong for the application to have intricate knowledge of library class internals. Is there a recommended method of doing what we are without "exposing ourselves"?

Here's a version of what I'm talking about in case my description lacked info or used poor terminology. The Test.C file would normally be part of the library.

class StaticTest {

class MyContainer {
    static StaticTest _list [3];
    static StaticTest _single;

#include "Test.h"

StaticTest::StaticTest() { cerr << "StaticTest" << endl; }
StaticTest::~StaticTest() {}

StaticTest MyContainer::_list[3];
StaticTest MyContainer::_single;

MyContainer::MyContainer() { cerr << "MyContainer" << endl; }
MyContainer::~MyContainer() {}

#include "Test.h"

StaticTest MyContainer::_list[3];
StaticTest MyContainer::_single;

int main (int, char *[])
    MyContainer container;
    return 0;

What you are trying to do is perfectly OK and normal..

I know of at least one platform (VxWorks) where these have to be treated specially because the platform requires it to be so.

I would like to know on what platform you tried to do this , so I can try to run it on the same and investigate the problem further C++Pro

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date