Bloomberg LP Interview Question
Software Engineer / DevelopersHi John,
Your 7a is already corrected by Cookie
Your '7b' is also incorrect.
---> " Myclass *myclass = new MyClass[10]; " here new will allocate memory and creates object as well, by calling its default constructor. During compilation, You will encounter 'No appropriate default constructor available' error.
Rest of the code is also error.
The correct answer to 7b is:
MyClass* mc[10];
for (int i=0; i < 10; i++)
mc[i] = new MyClass(i); <--- it will call user-defined constructor here.
Is it can be some thing like this for stack?
MyClass objMyClass[] = {MyClass(1), MyClass(2)};
Jey is right,
the way like MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10} is not correct.
you must do it like
MyClass myClass[10] = {MyClass(1),MyClass(2),...,MyClass(10)}
That is correct of myClass[10] = {1,2,3,4,5,6,7,8,9,10}
I tested in vc++ and it totally worked!
just remember aggregation initialization: Type MyArray[size] = {Elem1, Elem2,...};
e.x, int m[] = {1,3,5,7};
So,
On Stack: MyClass myArray[] = {MyClass(1), MyClass(2),...,MyClass(10)};
On Heap: MyClass* p[] = {new MyClass(1), new MyClass(2),..., new MyClass(10)};
Jey:
The correct answer for allocating the objects on heap would be using placement new operator i guess.
create a chunk of memory (char*) with normal new[]
offset it to point to account object using placement new
note placement new wont allocate any memory but calls ctor
in this way u dont need a array of pointers
deletion could be little tricky
first explicitly call the dtor for each object and delete the chuck as a whole
10) simplest answer is use RAII and lock the object creation, something like,
MyInstance* MyInstance::Instance()
{
Lock lock(m_dataMutex);
if(!pInstance)
{
pInstance = new MyInstance;
}
return pInstance;
}
but this is not multi-thread safe and also is less optimized. Read about double-check locking pattern to know more.
7a. The following code is working fine:
#include <iostream>
using namespace std;
class Any
{
public:
int var;
//Any() { }
Any(int v)
{
var = v;
}
Any(Any &q)
{
var = q.var;
}
void prnt()
{
std::cout << "\n Var: " << var << "\n";
}
};
void main()
{
Any m[10] = {1,2,3,4,5,6,7,8,9,10};
for(int x=0; x<10; x++)
m[x].prnt();
}
7a: Myclass myclass[10];
7b:
Myclass *myclass = new MyClass[10];
int i;
for (i = 0; i < 10; i++)
myclass[i] = new MyClass();
@John
Myclass myclass[10]; will not work.This statement would give a compile time error as the class lacks a default constructor.
To construct the array on stack it would be done like below
int i[] = {1,2,3,4,5,6,7,8,9,10};
MyClass myClass[10] = {1,2,3,4,5,6,7,8,9,10};
1. By default class has private access specifier and structure has public in C++.
- Cookie May 16, 20092. MyClass(const MyClass &)
{
}
3. To create a copy of existing object.
4. By default = does a shallow copy so if your class has pointers to some other data type except inbuilt one then a shallow copy would be done and it may result in dangling pointers.
5. const MyClass& operator = (const MyClass &)
{
}
6. So that chaining can be possible like below
x = y = z = 10;
8. The class destructor should never throw an exception. A destructor can be called in 2 ways.One when an object goes out of scope and 2nd when stack unwinding happens.So if a destructor is called due to stack unwinding and it throws an exception then C++ unexpected is called which calles terminate which terminates the whole application.Nothing is destroyed not even local objects.
9. Make class Singleton.
10. Use Mutex to warp the code where instantiation happens.
11. Operator new allocates raw memory only.It is similar to malloc() in C.
new first allocates raw memory using operator new and then calls constructor for the class.
12. bad_alloc exception is thrown.