Topic: The name of constructors and destructors


Author: NULL@NULL.NULL ("Tom s")
Date: Mon, 15 May 2006 19:19:08 GMT
Raw View
A good programmer strives to make their code as easily changeable as=20
possible. For instance, instead of writing:

int * const p =3D static_cast<int*>( malloc( 500 * sizeof(int) ) );

They will replace "sizeof(int)" with "sizeof(*p)", so that the code can=20
adapt as effortlessly as possible.

>From when I first learned C++, I couldn't see the logic in giving classe=
s'=20
constructors and destructor the same name as the class. Let's say we have=
 a=20
class which has 23 member functions, 7 constructors, and a destructor. If=
 we=20
have a change of heart and want to rename our class from "Chimpanzee" to=20
"Chimp", then a lot of "Find and Replace" is necessary.

The following is superior:

class Monkey {
public:

    Constructor()
    {

    }

    Destructor()
    {

    }
};


It also would not result in the peculiarity seen in the following code:=20

#include <string>
using std::string;

typedef string Monkey;

int main()
{
    char mem_block[ sizeof(Monkey) ];
   =20
    Monkey &monkey =3D *new(mem_block) Monkey;
   =20
          monkey.~Monkey();
   =20
          monkey.~string();
   =20
   =20
    /* Also: */
   =20
    string str;
   =20
        str.~Monkey();
}


-Tom=E1s

---
[ 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                      ]





Author: "Tom s" <NULL@NULL.NULL>
Date: Mon, 15 May 2006 16:37:28 CST
Raw View
"Tom   s" posted:


> class Monkey {
> public:
>
>     Constructor()


For even more adaptability, there could have been a "Class" keyword, which
could be a typedef for the class itself. It can implemented manually
though:

class Monkey {
public:

    typedef Monkey Class;

    Constructor( const Class& original ); /* Copy Constructor */

};

Now if the class's name changes, there's even less maintenance.


-Tom   s

---
[ 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                      ]





Author: nevin@eviloverlord.com ("Nevin \":-]\" Liber")
Date: Tue, 16 May 2006 06:17:58 GMT
Raw View
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                      ]