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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:36:19+00:00 2026-05-24T05:36:19+00:00

How to correct declare static vector in class? Currently I have crash on one

  • 0

How to correct declare static vector in class?
Currently I have crash on one line because vector initialize too late.

Sample one:

#include "stdafx.h"
#include <vector>    
class A{
private:
    int aValue;
public:
    static std::vector<A*> listOfA;

    A(int i)
    {
        aValue = i;
        A::listOfA.push_back(this); // !!! HERE crash in release mode only, in debug mode items add to vector, but remove when vector initialize
    }
};

A testA(1);
std::vector<A*> A::listOfA;



int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

Sample two:

classA.h

#include <vector>

class A{
private:
    int aValue;
public:
    static std::vector<A*> listOfA;

    A(int i);
};

classA.cpp

#include "stdafx.h"
#include "classA.h"

std::vector<A*> A::listOfA;

A::A(int i)
{
  aValue = i;
  A::listOfA.push_back(this); // !!! HERE crash in release mode only, in debug mode items add to vector, but remove when vector initialize
}

main.cpp

#include "stdafx.h"
#include "classA.h"

A testA(1);

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

In sample 2 if cpp files in project have this order(compilation order) all works fine:
classA.cpp
main.cpp

If order this, we have crash:
main.cpp
classA.cpp

  • 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-05-24T05:36:19+00:00Added an answer on May 24, 2026 at 5:36 am

    quoted from: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14
    There are many solutions to this problem, but a very simple and completely portable solution is to replace the global object listOfA, with a global function, listOfA(), that returns the object by reference.

    std::vector<A*>& listOfA()
    {
        static std::vector<A*> ans;
        return ans;
    }
    

    Since static local objects are constructed the first time control flows over their declaration (only), the above new listOfA() statement will only happen once: the first time listOfA() is called. Every subsequent call will return the same object. Then all you do is change your usages of listOfA to listOfA():

    int _tmain(int argc, _TCHAR* argv[])
    {
        // do stuff
        A::listOfA().dostuff();
        // do stuff
    }
    

    This is called the Construct On First Use Idiom because it does just that: the global Fred object is constructed on its first use.

    The downside of this approach is that the object is never destructed. There is another technique that answers this concern, but it needs to be used with care since it creates the possibility of another (equally nasty) problem.

    [Edit] Sorry nbt, didn’t see that you already linked to the faq. He deserves the credit[/Edit]

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

Sidebar

Related Questions

I have a class EnumConverter<T extends Enum<?>> that converts strings to the correct enum
I have declared a static method in class category public static function getPrefixFromSubCategoyId($subCategoryId) {
I want to have a static function which I declare in my .c file
What is the correct way to import a C++ class from a DLL? We're
Is it correct to link a static library (.lib) compiled with VS 2005 with
i have a static library which (among other things) implements a tiny function that
I have a stateless session bean which needs access to a factory class. Is
I am currently writting a JNI wrapper for a C++ class and I'm not
When I use static variables in C++, I often end up wanting to initialize
I am new to php. I was wondering how I could declare a static

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.