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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:09:59+00:00 2026-06-15T06:09:59+00:00

Can I do this: static Toggle GetAutoUpdatedToggle(DWORD key, bool initialState = false) { Toggle

  • 0

Can I do this:

static Toggle GetAutoUpdatedToggle(DWORD key, bool initialState = false)
{
    Toggle tempToggle(key, initialState);
    autoUpdateToggles.push_back(tempToggle); //This is static member - std::vector<Toggle>
    return tempToggle;
}

And I’m also using it later like that:

void Toggle::UpdateAllFromFactory() //This is static function
{
    for each (Toggle toggle in autoUpdateToggles)
    {
        toggle.Update();
    }
}

Is this good way of doing it?


UPDATE 1 – After your suggestoins:

static Toggle* GetAutoUpdatedToggle(DWORD key, bool initialState = false)
{
    Toggle *pToggle = new Toggle(key, initialState);
    m_autoUpdateToggles.push_back(pToggle);
    return pToggle;
}

void Toggle::UpdateAllFromFactory()
{
    for (std::vector<Toggle*>::iterator it = m_autoUpdateToggles.begin(); it < m_autoUpdateToggles.end(); it++)
    {
        (*it)->Update();
    }
}
  • 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-15T06:09:59+00:00Added an answer on June 15, 2026 at 6:09 am

    No, this is not a good way of doing it, because you pass around copies of Toggle:

    • GetAutoUpdatedToggle returns a copy of the Toggle that it just pushed into the vector. It’s not in itself a wrong thing to do, but any manipulations the caller may do on the returned toggle would not be reflected on the one you pushed onto the vector
    • The for loop goes through elements of the vector, creating a copy for use inside the loop body. Unless the Toggle itself has a pointer-like semantic, the Update() action would not be reflected on Toggle objects inside the vector.

    To fix this issue, make GetAutoUpdatedToggle return a reference to the Toggle object that it just pushed onto the vector, and use a vector<Toggle>::iterator object to iterate through the stored toggles. This would let you operate on the actual objects, rather than on their copies.

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

Sidebar

Related Questions

Statement: Static member variables can be ONLY changed by static methods. Is this statement
Right now I can use this URL to request a Google Static Maps image
For example can i have something like this: public static void SAMENAME (sameparameter n)
in java we can do this: public class A{ public static void main(String...str){ B
How can i make an extension method that will work like this public static
Why you can do this in C#? class Test { static void Main() {
In. NET, I can do something like this: public static T[] CreateAndFillArray<T>(int size) where
Can you clarify this please? public static void MyMethod() { var context= new MyModel.Entities();
Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2
Can this be done quickly in jQuery before I start looping through them? Or

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.