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 8643703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:06:58+00:00 2026-06-12T12:06:58+00:00

In the book The C++ Programming Language, by Bjarne Stroustrup, the author says: Sometimes

  • 0

In the book The C++ Programming Language, by Bjarne Stroustrup, the author says:

Sometimes when you design a library, it is necessary, or simply convenient, to invent a type with a constructor and a destructor with the sole purpose of initialization and cleanup. Such a type would be used once only: to allocate a static object so that the constructor and the destructor are called. For example:

 class  Zlib_init
{
    Zlib_init() ; //get Zlib ready for use
   ~Zlib_init()  ; //clean up after Zlib
};
Class Zlib
{
   static  Zlib_init   x;
   /  /...
};

Unfortunately, it is not guaranteed that such an object is initialized before its first use and destroyed after its last use in a program consisting of separately compiled units.

Why does the author keep the constructor and destructor as private members?
And why won’t this method work if we use it in a program consisting of seperately compiled units?
Won’t it require definition of the member x for calling the constructor Zlib_init() and destructor ~Zlib_init()? Then what is the use of this method?
It’s in section 10.4.9 of the book.

  • 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-12T12:06:59+00:00Added an answer on June 12, 2026 at 12:06 pm

    Why does the author keep the constructor and destructor as private members?

    The constructor & destructor being private seems to be a typo.
    Class static members needs to be defined in order that you can use them. In order that the static member x is defined the constructor needs to be accessible. if not the linker will complain about undefined reference.

    Online Sample:

    class  Zlib_init 
    {    
       Zlib_init() ; //get Zlib ready for use  
      ~Zlib_init()  ; //clean up after Zlib 
       public:
         int j;
    };
    class Zlib 
    {  
       public:  
       static  Zlib_init   x;    
    }; 
    
    Zlib_init Zlib::x;
    
    int main()
    {
        Zlib::x.j = 10;
    
        return 0;
    }
    

    Output:

    prog.cpp:3: error: ‘Zlib_init::Zlib_init()’ is private     
    prog.cpp:14: error: within this context      
    prog.cpp: In static member function ‘static void Zlib::__static_initialization_and_destruction_0(int, int)’:     
    prog.cpp:4: error: ‘Zlib_init::~Zlib_init()’ is private     
    prog.cpp:14: error: within this context     
    

    And why won’t this method work if we use it in a program consisting of seperately compiled units?

    If you fix the typo mentioned above by making the constructor and destructor public or by making Zlib a friend of class Zlib_init the code still faces another problem.
    The problem is popularly known as Static Initialization Fiasco in C++.

    Good Read:

    [10.14] What’s the “static initialization order fiasco”?
    [10.17] How do I prevent the “static initialization order fiasco” for my static data members?

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

Sidebar

Related Questions

In the book The C++ Programming Language, by Bjarne Stroustrup, the author introduces a
In Bjarne Stroustrup's book The C++ Programming Language, it is stated that a derived
In the book The C Programming Language it says: Many of the functions in
In section 7.1.1 of the book The C++ Programming Language the author states: inline
I'm learning C++ and the book I'm reading (The C++ Programming Language) says to
This is taken right from The C++ Programming Language by Bjarne Stroustrup. I would
In Stroustrup's C++ Programming Language book (3rd edition), in the Numerics chapter he shows
In The C++ Programming Language book, in list of operations (article 6.2) , Bjarne
I've been working through Bjarne Stroustrup's The C++ Programming Language (2nd edition - I
In the book of The C++ Programming Language, the author gives the following example

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.