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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:22:43+00:00 2026-06-09T13:22:43+00:00

I have trouble with these static members initialization of c++ classes. See my code

  • 0

I have trouble with these static members initialization of c++ classes.
See my code for more info.

Source

header.h

#ifndef HEADER_H
#define HEADER_H
#include <string>
using namespace std;
class Staff{ public: static string str;};
class Boss{ public: static string str;};
#endif

staff.cpp

#include "header.h"
string Staff::str = "(staff)";

boss.cpp

#include "header.h"
string Boss::str = "I call " + Staff::str;

main.cpp

#include <iostream>
#include "header.h"
int main(){cout << Boss::str << endl;}

And here are many compile codes with different results:

Pre-compile:

g++ -c boss.cpp
g++ -c staff.cpp
ar rcs lib.a boss.o staff.o
ar rcs rlib.a staff.o boss.o

Compile, run and result:

g++ main.cpp staff.cpp boss.cpp ; ./a.out
==> I call (staff)
g++ main.cpp boss.cpp staff.cpp ; ./a.out
==> segmentation fault (core dumped)
g++ main.cpp lib.a ; ./a.out
==> segmentation fault (core dumped)
g++ main.cpp rlib.a ; ./a.out
==>segmentation fault (core dumped)

I want to use library archive instead of confusing with giant objects order when compiling.
Help me to solve them.

Thank you.

  • 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-09T13:22:45+00:00Added an answer on June 9, 2026 at 1:22 pm

    The initialization order of static variables in separate translation units is undefined. Your two source files make up two separate translation units, and each one defines one variable. The segmentation fault probably occurs while attempting to use Staff::str to initialize Boss::str when Staff::str hasn’t been initialized yet.

    To solve it, define them both in the same single translation unit:

    #include "header.h"
    string Staff::str = "(staff)";
    string Boss::str = "I call " + Staff::str;
    

    Or make their initializations independent of each other:

    std::string Staff::get_str() {
      return "(staff)";
    }
    
    string Staff::str = Staff::get_str(); 
    
    string Boss::str = "I call " + Staff::get_str();
    

    From your first two examples, it appears that the initialization order is related to the linking order, but you mustn’t rely on that.

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

Sidebar

Related Questions

I have trouble finding way to correctly refactor this code so that there would
While studying C# in ASP.net I have trouble understanding several classes. In which scenario
(iOS) I have a view that contains multiple textviews under static headings (see image
I am having some trouble with compiling my code due to these error messages
I have trouble choosing the right audio playback technology. There's a ton of technologies
I have some trouble with jquery ui-events: In my application, there are some sliders.
I have an query that has been giving me trouble. I think there is
I have trouble figuring out why a simple division like this one always returns
We have trouble implementing a nice session mechanism in JSP. One of our requirement
I have trouble with strange behavior of for..in cycle in JavaScript. I have the

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.