Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7957363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:14:21+00:00 2026-06-04T04:14:21+00:00

I am trying to get the following setup right: A given application (with multiple

  • 0

I am trying to get the following setup right:

A given application (with multiple source files, compilation units) has global variables of type class A defined in many compilation units.
These should be “managed” by a new to introduce class B (where only 1 instance should exist) in the sense that upon creation they “register” themselves at instance of class B and at destruction “sign off”.

Setting the thing up for the constructors to work is fairly straight-forward. One can use:

types.h:

class B {
  static B& Instance() {
    static B singleton;
    return singleton;
  }
  void registerA( const A& a ) { 
  // whatever
  }
};


class A {
  A() { B::Instance().registerA( this ); }
};

How to get the destructors right? If using:

class A {
  A() { B::Instance().registerA( this ); }
  ~A() { B::Instance().signoffA( this ); }
};

then the destructor of B might be called before the destructor of A.
Then the instance of class A signs off at a just newly created instance of B.

The test case would be a multi source file setup with definitions of instances of class A in a namespace:

file1.cc

#include "types.h"
namespace C {
   A a;
}

file2.cc

#include "types.h"
namespace C {
   A b;
}

I guess on can do such thing easily with Boost smart pointers. However, if possible I would like to avoid using additional libraries to keep dependence as low as possible.

One thing that might help: All global variables are in a named namespace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:14:22+00:00Added an answer on June 4, 2026 at 4:14 am

    I think you’re fine. Here’s 3.6.3 on “Termination”:

    If the completion of the constructor or dynamic initialization of an object with static storage duration is sequenced before that of another, the completion of the destructor of the second is sequenced before the initiation of the destructor of the first.

    Suppose you have the following setup:

    struct A;
    
    struct B
    {
        static B & get() { static B impl; return impl; }
        void registrate(A *);
    
    private:
        B() { /* complex stuff */ }
        // ...
    };
    
    struct A { A() { B::get().registrate(this); } };
    
    A a1;
    

    Now whatever happens, the first constructor of a static A-type object will call B::get(), which sequences the construction of the static impl object before the completion of the first A-constructor. By the above clause, this guarantees that the destructor of the B impl-object is sequenced after all the A-destructors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to setup my swap chain Buffer but I get the following
I get the following error when trying to run my application. There are no
I've been battling to get this right...basically I have the following HTML setup: <div
I am trying to get the following code to work: if (isset($_POST['file'])){ if(file_exists($_POST['file'])){ echo
I'm trying to get the following regex to work on my String: Pattern Regex
I am trying to get the following form submission to show alert box if
As the title says, I am trying to get the following Javascript working in
I get following error when trying to access Xampp from a network I've tried
I get the following problem when trying to open an ASPX page in FireFox:
I get the following exception when trying to access any nodes of a parsed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.