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].