Bloomberg LP Interview Question for Software Engineer / Developers






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

Here is the solution


class A
{
public:
A(int i)
{

}
private:
A() {}
~A() {}

public:
static A* instance()
{
return new A();
}

static void free_instance(A* a)
{
delete a;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
A a1(2); // error C2248: 'A::~A' : cannot access private member declared in class 'A'

A* aa = A::instance();

A::free_instance(aa);
}

- Boris May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

pretty cool!

- xankar June 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Declare private destructor for class A....

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

If you do this how will you ever create object to this class..
The question is to avoid object creation on the stack, we should still be able to create objects on the heap

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

@Tito
Isn't it possible by making class static?

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

I don't think you can make a class static in C++...Also he only wants that object should not be created on stack but can be created on heap. one can't declare a constructor static only member var and function can be, which will not solve our problem...

- tito April 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

add a explicit keyword before the class constructor. Then the constructor with single argument wouldn't go for implicit conversion.

- Manish Agarwal April 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Manish
Explicit won't help you in this case. Also adding a explicit key word doesn't mean you won't be able to put the object on stack.
Explicit is helpful in a case of "Conversion constructor".
So calling constructor " A a(1) ;" doesn't make any difference with or without explicit .
but when " A a =1; " is used, adding explicit with constructor will not allow to create the object this way but you can still create the object on stack..

- tito April 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Add default parameters with the private ctor for A.
like "A(int x=0,int y=0){};"

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

It doesn't solves the problem....
what if I pass object as argument rather than normal data types....

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

you just put the class definition in a local scope

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

Could you please give a sample code how to achieve this.....

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

Can't we just declare the new constructor to be private? also is this is abstract class, cause in the first place we cannot create object of it anywaz

- jack.watermelon April 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create another class lock with private constructor.
Drive your class "A" with it.

class lock
{
  private:
  lock()
  {}
};
class A: public lock
{
  // Any constructor
};

This disables the instantiation of your class "A"

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

Dont understand how it works. What do you mean?

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

I think ashutosh is speaking with respect to Class A, where it will not allow to create object in stack..But either it will not allocate object through heap also.

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

I think ashutosh is speaking with respect to Class A, where it will not allow to create object in stack..But either it will not allocate object through heap also.

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

How about this? Create another class (Foo) and have its no argument constructor private. Derive the given class A from the above create Foo class. However this will prevent any constructors of class A to work. I am not sure what the intent is.

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

Thats what even Ashutosh above is also mentioning, but this will NOT allow you to create object even through heap also. The question if i understand, he dont want to create in Stack but only in heap.

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

class A 
{
public:
	A(int i)
	{

	}
private:
	A() {}
	~A() {}

public:
	static A* instance()
	{
		return new A();
	}

	static void free_instance(A* a)
	{
		delete a;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	A a1(2); //  error C2248: 'A::~A' : cannot access private member declared in class 'A'

	A* aa = A::instance();

	A::free_instance(aa);
}

- Boris May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Very good solution!
Private destructor plus static constructor/destructor idiom...
Just one little addition: we also need to make our "new" operator as private,
else we still can create an object using "new"!

- sergey.a.kabanov January 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

add a protected destructor

- elligno May 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think creating at least one pure virtual function makes the creation of object for the class impossible.

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

As @Ashutosh explained, use non-copyable base class. Refer boost for details.

- Venki May 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class A {
int a_;
~A() {}

public:
A(int a) :a_(a) {}
void destroy() { delete this; }
};

int main()
{
A a(1); //can't call as destructor private
A *ptr = new A(1);
ptr->destroy();
return 0;
}

- Raju October 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try abstract class. In this case try define destructor as pure virtual

- Peter October 09, 2012 | 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