Constructor with static members in library

Question:
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 inonly 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 thelibrary.

Test.h ———-class StaticTest {public:    StaticTest();    ~StaticTest();};class MyContainer {public:    MyContainer();    ~MyContainer();private:    static StaticTest _list [3];    static StaticTest _single;};Test.C ———-#include #include “Test.h”StaticTest::StaticTest() { cerr << "StaticTest" << endl; }StaticTest::~StaticTest() {}#if LOOKS_GOOD_BUT_DOESNT_WORKStaticTest MyContainer::_list[3];StaticTest MyContainer::_single;#endifMyContainer::MyContainer() { cerr << "MyContainer" << endl; }MyContainer::~MyContainer() {}Main.C -----------#include "Test.h"#if WORKS_BUT_FEELS_WRONGStaticTest MyContainer::_list[3];StaticTest MyContainer::_single;#endifint main (int, char *[]){    MyContainer container;    return 0;}

Answer:
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 furtherC++Pro

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.