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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:03:06+00:00 2026-06-10T02:03:06+00:00

I learned long ago that the only reliable way for a static member of

  • 0

I learned long ago that the only reliable way for a static member of be initialized for sure is to do in a function. Now, what I’m about to do is to start returning static data by non-const reference and I need someone to stop me.

function int& dataSlot()
{
    static int dataMember = 0;
    return dataMember;
}

To my knowledge this is the only way to ensure that the static member is initlized to zero. However, it creates obscure code like this:

dataSlot() = 7; // perfectly normal?

The other way is to put the definition in a translation unit and keep the stuff out of the header file. I have nothing against that per se but I have no idea what the standard says regard when and under what circumstances that is safe.

The absolute last thing I wanna end up doing is accidently accessing uninitialized data and losing control of my program.

  • 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-10T02:03:08+00:00Added an answer on June 10, 2026 at 2:03 am

    Returning a non-const reference in itself is fairly harmless, for example it’s what vector::at() does, or vector::iterator::operator*.

    If you don’t like the syntax dataSlot() = 7;, you could define:

    void setglobal(int i) {
        dataSlot() = i;
    }
    int getglobal() {
        return dataSlot();
    }
    

    Or you could define:

    int *dataSlot() {
        static int dataMember = 0;
        return &dataMember;
    }
    
    *dataSlot() = 7; // better than dataSlot() = 7?
    std::cout << *dataSlot(); // worse than std::cout << dataSlot()?
    

    If you want someone to stop you, they need more information in order to propose an alternative to your use of mutable global state!

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

Sidebar

Related Questions

Learned all about computing algorithm costs in College, but that was so long ago
Even though I am a long time C programmer, I only recently learned that
a long time ago I learned that this is a bad idea. Please don't
I just learned about comet pattern(Long polling) and very curious to implement a simple
I learned about $_SESSION about several weeks ago when creating a login page. I
I learned the object-oriented in Java. Now in develop in C#. This means that
I learned something about DLL's yesterday. But I haven't found the way how to
I learned HTML and CSS about 8 months ago, and recently, about 2 months
Long ago I have learned sql and within the last years of application development
I recently learned of the IIF(A,B,C) function. I'm a long time VB/VB.NET Coder who

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.