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

  • Home
  • SEARCH
  • 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 7502767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:59:03+00:00 2026-05-29T20:59:03+00:00

I need to create a vector which holds the pointers to some static objects

  • 0

I need to create a vector which holds the pointers to some static objects (e.g., a1, a2, a3).

void foo()
{
    static TEST a1, a2, a3;
    static vector<TEST *> m_test;
    m_test.push_back( &a1 );
    m_test.push_back( &a2 );
    m_test.push_back( &a3 );
}

But I don’t want to have to write out the declaration for each static object.

How can I use a loop to do this? I have tried something like:

    for (unsigned int i=0; i<10; i++)
    {
        m_test.push_back( &(static TEST()));
    }

But it seems the static doesn’t have any effect inside an expression, as the objects get destroyed, as I’d expect from a normal temporary.

  • 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-29T20:59:06+00:00Added an answer on May 29, 2026 at 8:59 pm

    Function-static objects may only be created by a defining declaration with a name. You can’t create a “function-static temporary” like that.

    How about:

    static const size_t N = 10;
    static TEST tests[N];
    
    for (size_t i = 0; i < N; i++) {
       m_test.push_back(&tests[i]);
    }
    

    Otherwise, you have no option but to resort to dynamic-storage objects, the only objects whose very existence in any given scope can be controlled programmatically (not least because they’re not really in any scope at all). In fact, this is pretty much the use case for objects of dynamic storage duration.

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

Sidebar

Related Questions

I have written a class (AbcdBase) which holds several static objects, these include maps
I need to create a function which appends a value to a vector and
I have a vector of doubles and I need to create another array which
Need to create a custom DNS name server using C which will check against
I need to create an ASP page (classic, not ASP.NET) which runs remote shell
I need to create an up vector for when the camera looks at any
I have an assignment in which I need to create a function that tells
I'm working with vector now and I have an interesting situation which I need
i need to write a program in which main() would read a file(containing some
I've got a std::vector which I need to sort by selected algorithms for certain

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.