Re: The name of constructors and destructors
 
In article <m94ag.9208$j7.305481@news.indigo.ie>,
 NULL@NULL.NULL ("Tom?s") wrote:
A good programmer strives to make their code as easily changeable as 
possible. For instance, instead of writing:
int * const p = static cast<int*>( malloc( 500 * sizeof(int) ) );
They will replace "sizeof(int)" with "sizeof(*p)", so that the code can 
adapt as effortlessly as possible.
And if the type changes to something with non-trivial construction or 
destruction, this construct allows you to introduce a bug "as 
effortlessly as possible."
Unless you have special requirements, there are better alternatives to 
malloc.  Consider:
int p[500];
std::vector<int> const vi(500);
int* const p = &vi[0];
int* const p = new int[500];
From when I first learned C++, I couldn't see the logic in giving classes' 
constructors and destructor the same name as the class. Let's say we have a 
class which has 23 member functions, 7 constructors, and a destructor. If we 
have a change of heart and want to rename our class from "Chimpanzee" to 
"Chimp", then a lot of "Find and Replace" is necessary.
Unless this class is totally unused, you'll have a lot of "Find and 
Replace" necessary at all the call sites.  With seven different 
constructors declared, I'd expect a class like this to already have been 
used a lot.
It also would not result in the peculiarity seen in the following code: 
#include <string>
using std::string;
typedef string Monkey;
int main()
{
    char mem block[ sizeof(Monkey) ];
    
    Monkey &monkey = *new(mem block) Monkey;
    
          monkey.~Monkey();
    
          monkey.~string();
    
    
    /* Also: */
    
    string str;
    
        str.~Monkey();
}
In most code, placement new is a rare thing.  Do you observe otherwise?
-- 
 Nevin ":-)" Liber  <mailto:nevin@eviloverlord.com>  (773) 961-1620
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]