Bloomberg LP Interview Question
Software Engineer / DevelopersNo, i think 3 constructors and destructors for test1 and 4 constructors and destructors or test2.
in test 2
1. for called for opassing an object to a funs cy value.
2. for retrurning the object by refrence(in func).
3. for initializing a
4.
can anybody explain how r 3 constructors getting called in test1 i can see only 2 one being default constructor and another copy constructor which copies by value
Test1 :
1) default will be called at Base a
2) Copy constructor will be called when a is passed to func
3) Copy constructor will be called when b is returned from func
Test2 :
1) default will be called at Base a
2) Copy constructor will be called when a is passed to func
3) Copy constructor will be called when b is returned from func
Test1
Step-1 Default constructor will be called at Base a;
Step-2 Copy constructor will be called when passing a as formal parameter i.e func(a).
Step-3 Copy constructor will be called for return object.
Step-4 Destructor will be called for actual parameter of func.
Step-5 Now this return obj will act as nameless obj in test1() and will get destroy after line func(a) calling destructor one more time.
Step-6 Finally Destructor will be called for a.
You can see same for test2();
If you want to sound real smart in your interview, you can say that the answer to 3 depends on whether your compiler employs RVO (Return Value Optimization), which it likely thus. In this case, of the Unnamed variety.
It would be 4 otherwise (1 default and 3 copy constructors). But the compiler sees the following
Base b = func(a);
Base func(Base c){return c;}
and rewrites it as
Base b(c);
Simply put, the compiler will construct the temporary inside the space saved for object b.
3 cons and 3 dest for each function
- Maankutti September 29, 2009Test1
Base()
Base(const Base&)
Base(const Base&)
~Base()
~Base()
~Base()
Test2
Base()
Base(const Base&)
Base(const Base&)
~Base()
~Base()
~Base()