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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:23:14+00:00 2026-05-12T10:23:14+00:00

I am fairly new to c++, especially in its techniques. My question is, how

  • 0

I am fairly new to c++, especially in its techniques. My question is, how can I create a static object member of a class itself. What I mean is I declared a static member object inside a class. Example:

CFoo:CFoo *pFoo[2] = {0};

class CFoo
{
   public: static CFoo *pFoo[2];
   public: CFoo(int a);
   public: CFoo *getFoo();
};

Now the problem is, how can I create the pFoo, like I want to create two static object pFoo,

pFoo[0] = new CFoo(0);
pFoo[1] = new CFoo(1);

so that I can use the getFoo method to return one of the pFoo, like,

CFoo *getFoo()
{
   return pFoo[0]; //or pFoo(1);
}

Thanks alot guys. Hope my questions are clear.

Thanks again in advance.
-sasayins

  • 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-12T10:23:14+00:00Added an answer on May 12, 2026 at 10:23 am

    Let’s improve your code one step at a time. I’ll explain what I’m doing at each step.

    Step 1, this isn’t Java. You don’t need to specify public for every member. Everything after public: is public until you specify something else (protected or private). I also moved the definition of pFoo after the class. You can’t define a variable before it’s been declared.

    class CFoo
    {
       public: 
          static CFoo *pFoo[2];
          CFoo(int a);
          CFoo *getFoo();
    };
    
    CFoo* CFoo::pFoo[2] = {0};
    

    Step 2, pFoo probably shouldn’t be public if you’re going to have a getFoo member function. Let’s enforce the interface to the class instead of exposing the internal data.

    class CFoo
    {
       public: 
          CFoo(int a);
          CFoo *getFoo();
    
       private:
          static CFoo *pFoo[2];
    };
    
    CFoo* CFoo::pFoo[2] = {0};
    

    Step 3, you can return by pointer without bothering to use new. I’ve written C++ code for many years, and I’d have to look up how you delete the memory that was newed for a static member variable. It’s not worth the hassle to figure it out, so let’s just allocate them on the stack. Also, let’s return them by const pointer to prevent users from accidentally modifying the two static CFoo objects.

    class CFoo
    {
       public: 
          CFoo(int a);
          const CFoo *getFoo();
    
       private:
          static CFoo foos[2];
    };
    
    CFoo CFoo::foos[2] = {CFoo(0), CFoo(1)};
    

    The implementation of getFoo then becomes:

    const CFoo * CFoo::getFoo()
    {
       return &foos[0];  // or &foos[1]
    }
    

    IIRC, the static member foos will be allocated the first time you create a CFoo object. So, this code…

    CFoo bar;
    const CFoo *baz = bar.getFoo();
    

    …is safe. The pointer named baz will point to the static member foos[0].

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

Sidebar

Related Questions

I am fairly new to C++ and especially object-oriented design of classes. I want
Being fairly new to C++ I have a question bascially concerning the g++ compiler
I'm fairly new to asp.net and especially LINQ and SQL. Say I have a
Fairly new to python, forgive me if this is a basic question about learning
Im fairly new to c sorry if this is a stupid question! i have
Fairly new to iPhone/Cocoa development, so I'm having difficulty adjusting. I want to create
fairly new iPhone developer here. Building an app to send RS232 commands to a
Fairly new to Objective C and trying to work within Storyboards given it's the
Fairly new to Azure and the whole worker role concept, previously if I wanted
I fairly new to JQuery and perhaps trying to achieve something that might be

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.