Bloomberg LP Interview Question for Interns


Country: United States
Interview Type: In-Person




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

In C++ you can have virtual destructors but NOT virtual constructors. This is because when an object has virtual functions it must have an associated v-table which keeps track of the addresses to the virtual functions for that class. That would mean that an object must be instantiated firstly before a v-table for that object could exist. Since constructors are intended to instantiate and create the object there can be no virtual constructor.

On the other hand a virtual destructor is allowed and should be used when ever there are virtual methods in the base class. The misuse of virtual destructors can lead to memory leaks and bad side effects

- Rahul September 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

bkumar.kcumca doing upvote of Rahul's post right now everywhere

good jobs to Rahul for self upvote

- anon November 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

--- but NOT virtual constructors

it's not true. Borland Builder C++ has virtual constructor to support Delphi VCL library

- LBaralgeen March 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

V-table are built per class (not per object). That is not the correct reason for not having virtual constructor.
The reason is that compiler must know the object type at compile time (which is violated in case of virtual constructor). Hence there cannot be virtual constructors.

- nharryp December 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider this:
Base * a;
a = new Derived();
The v-table mechanism comes into picture after the base class pointer is set to the derived class object and calls something e.g a virtual function. However, for the base class pointer to be set to the derived class object, the object has to exist in the first place. Hence, making the constructor virtual would create a kind of deadlock. Anyway, there is no need for a virtual constructor.

- nharryp January 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

/* using Microsoft Visual Studio 2010 */

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

//#define __NO_VIRTURAL_DESTRUCTOR
typedef unsigned int Positive;


class Base
{
   	public:
      	Base(){ cout<<"Constructing Base\n";}
      	
#ifdef __NO_VIRTURAL_DESTRUCTOR	
    // this is a destructor:
	~Base(){ cout<<"Base destructor called\n";}
#else
	// this is a virtual destructor:
	virtual ~Base(){ cout<<"Base virtual destructor called\n";}
#endif

};

class Derive: public Base
{
        public:
       	Derive(){ cout<<"Constructing Derive\n";}
       	
       	~Derive(){ cout<<"Destroying Derive\n";}
 };


/*
 * programmerinterview_dot_com/index.php/c-cplusplus/virtual-destructors/
 * Well, what we can do is make the base class destructor virtual, 
 * and that will ensure that the destructor for any class that derives 
 * from Base (in our case, its the "Derive" class) will be called. 
 */

void main()
{
    	Base *basePtr = new Derive();
        
        delete basePtr;
		cin.get();
}

/* results: using virtual destructor
 * Constructing Base
 * Constructing Derive
 * Destructing Derive
 * Base virtual destructor called
 *
 * results without using virtual destructor:
 * Constructing Base
 * Constructing Derive 
 * Base virtual destructor called
 */

- JimMob October 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Methods like Init and Dispose. Not the actual constructors, but are use to construct and dispose the objects. They are virtually constructors and destructors.

HAHAHAAHAHAHAHAHA.

- Anonymous September 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This sample program shows that the second paragraph of the answer is not correct.

- JimMob October 02, 2013 | 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