Friday, January 28, 2011

Allocation of an array of objects lacking default constructors in C++

All of us know that in C++ when we wish to create an array of objects of some class T then T must have a default constructor. But what if, the default constructor is not available in class T? Is there any way you could create an array of objects which have parametric constructors?

Lets consider an example:

  1: class Account
  2: {
  3: private:
  4:  double m_security;
  5:  double m_principal;
  6:  float m_rate;
  7: 
  8: public:
  9:  Account(double security);
 10:  double calculateInterest(int time, float rate);
 11:  void deposit(double amount);
 12:  void withdraw(double amount);
 13:  double checkBalance();
 14: };
Can you write :
Account typeAaccount[100];

Or something like this:
Account typeBaccount[50](500);

Well second option as we all know is outrageously wrong! Nevertheless first option is wrong either...
Had there be no constructor at all, your faithful compiler would have provided a default constructor and would have made the array allocation possible...But now it will shout with a compile-time error. Well you can go all the way round, add a static variable, set its value before each array allocation and use it in your parameter-less default constructor. That is to say:
  1: class Account
  2: {
  3: private:
  4:  double m_security;
  5:  double m_principal;
  6:  static int security;
  7: public:
  8:  Account();
  9:  double calculateInterest(int time, float rate);
 10:  void deposit(double amount);
 11:  void withdraw(double amount);
 12:  double checkBalance();
 13: };
 14: double Account::security = 0.0;
 15: Account::Account()
 16: {
 17:  m_security = security;
 18:  m_principal = security;
 19: }
Now you can create the array, supplying the argument through static variable:
Account::security = 1000.0;
Account typeAaccount[100];
Account::security = 500.0;
Account typeBaccount[100];

But that said, there can be a solution as elegant as this for dynamically allocated object arrays:
  1: char* storage = new char[100 * sizeof(Account)];
  2: Account* pvui = (Account*)storage;
  3: 
  4: for(int i=0; i<100;++i)
  5: {
  6:  new(pvui + i)Account(500);
  7: }
  8: 
  9: // use the array of Account objects
 10: 
 11: // call destructors now 
 12: for(int i=0; i<100;++i)
 13: { 
 14:  (pvui + i)->~Account(); 
 15: }
 16: 
 17: // finally delete the allocated buffer. 
 18: delete[] storage; 

@Shailesh: I must confess, your challenge "What's so special in this piece of C++ code" held me in awe for an hour... :)
Well if anybody like me is surprised to see the special "new", I must dive in details. That's placement new operator and takes a pointer to an already allocated memory chunk. So you can allocate a raw memory and then construct you objects over it calling the constructor in a loop. Since the objects are adjacent to each other you can use them wherever you would have used them as an array.
Happy!!!
And yes destructors can be called explicitly. Do take care and call them to clean up the mess you might have created and finally deallocate the raw memory buffer.

As easy as this!

No comments: