0

I have not found right answer though i tried multiple search in this site. I raise the question again.

Normally, the static object member should be initialized in the source file.

//header file
class A{ 
private: 
   static B*  bPoint ;

public: 
    static void init(int argc, char** argv);
  ... 
};

//Source file: 
B A::bPoint = new B()              //Normally, this should OK. 

But the problem is that default B construction is private and now I have to use another public construction

 B(int argc, char** argv); 

In this case, new B() would give out compilation error.. then how I can initialize the static bPoint in class A?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
shijie xu
  • 1,975
  • 21
  • 52
  • 1
    What is your problem with private: B() public: B(int argc, char** argv) ? (PS: Avoid pointers) –  Dec 03 '13 at 21:19
  • @DieterLücking. The reason of private: B() is that we don't want to create B varibale. E.g, Singleton. – shijie xu Dec 04 '13 at 23:29

1 Answers1

1

The static member should be defined in the source file, but note that you can initialize it with a null pointer at first:

// source file:
B* A::bPoint;  // note the pointer to B

Then, if your problem is to forward the arguments to A::init() to the constructor of B(), you could allocate an object for bPoint inside A::init():

A::init(int argc, char** argv)
{
    bPoint = new B(argc, argv);
}
blackbird
  • 957
  • 12
  • 18