Bloomberg LP Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

It is a good programming practice to keep the destructor virtual if your base class has any virtual function that is overridden in derived class.

Yes I do believe that there is need to keep the destructor virtual in the above when you have not allocated anything on the heap. Bcos objects on heap would clean up on its when when the derived class goes out of scope

- Harish March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I completely agree with this statement :" It is a good programming practice to keep the destructor virtual if your base class has any virtual function that is overridden in derived class."

But as far as I know virtual function are implemented using vTable which is late binding. and should be implemented on heap. So there is no question of having virtual function.

Also the last sentence is bit confusing. Could you please clarify it.

- Anonymous March 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If base class or derived class object is not created dynamically then there is no need for virtual destructor......

- Amit March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

still need virtual destructor

- Anonymous March 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please tell us why we need virtual destructor if there is no dynamic allocation?

- Anonymous March 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

run following code:

class Base{
public:
~Base(){cout<<"Base::~Base()"<<endl;}
};

class Derived:public Base{
public:
~Derived(){cout<<"Derived::~Derived()"<<endl;}
};

int main(){
Derived d;
Base* p = new Derived;
delete p;
cout<<"======="<<endl;
}

Derived object created on stack, Derived, Base destructor will be called in order
However, Derived object created on heap, the behavior is UNDEFINED! Therefore, even no members that are allocated on heap, and none virtual destructor for Base works fine on your machine and compiler, you cannot guarantee it works correctly on other machine and compiler.

- Anonymous March 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It has no problem to run the above code under MSVC2008

- creation May 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Say there is an static data member "counter" in Derived d, and the destructor of Derived will run "counter--". when you delete the d using Base*, the destructor of d will not be called, so "counter--" is not executed. I believe that's the reason why virtual dctor should be used even if nothing is on heap.

- Anonymous May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

No. it is not required if the members are not allocated dynamically.But as a standard any class eligible to be a base class needs to have virtual destructor.

- Karthik March 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But how about static member data? If the derived class has a static member used to record number of instantiated objects, the number increments in its constructor, and decrements in destructor. If only the base class destructor be called, that static member will not work correctly.

- Vivi April 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

..."members that are allocated on heap" are not the only resources one must free up properly in a destructors. Graphoical resources, n/w (e.g. open sockets), mutexes etc: why do you for get ab. all these?

- Anonymous April 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree with this one

- Creation May 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- siva.sai.2020 May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Agree with Anonymous on April 25, 2010.

In addition, Derived's data members won't be destroyed properly. Their destructors will not get called which is bad.

- Anonymous June 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we are assured of that there will be no use of heap then we should go for no-virtual destructor because using virtual destructor will cause an extra vtbl pointer in the object which do not have any need in this situation except that it will increase the processor instruction and pollute cahce.

- raja August 06, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More