Interview Question
Software AnalystsCountry: India
Only pointers can be null. As static object B::a was constructed by default CTOR even prior main().
You can see this line:
A's constructor called.
Copy of static object B::a will be constructed and returned from b1.getA(). This was copy CTOR will be called.
But in the line "A a1 = b1.getA(); " the copy of static object returned is copied in "a1" object , and first a1 object should be constructed using a default constructor of class A, correct me if i am wrong
Theoretically, temporary object will be created and returned from b1.getA(), and then in "A a1 = b1.getA();" copy CTOR will be called again to construct a1.
In practice creation of temporary object will be optimized by compiler.
What you describe is operation of assignment operator (CTOR can't be called twice):
A a1;
a1 = b1.getA();
Seems is not, nothing is pointed to a n A object reference since that may getA is may pointed to null
- Uriel Rodriguez February 17, 2014