C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The name of constructors and destructors

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Tomás
Guest





PostPosted: Mon May 15, 2006 8:21 pm    Post subject: The name of constructors and destructors Reply with quote



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.

Quote:
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.

The following is superior:

class Monkey {
public:

Constructor()
{

}

Destructor()
{

}
};


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();
}


-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 ]
Back to top
Tomás
Guest





PostPosted: Mon May 15, 2006 10:21 pm    Post subject: Re: The name of constructors and destructors Reply with quote



"Tomás" posted:


Quote:
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 ]
Back to top
Nevin \":-]\" Liber
Guest





PostPosted: Tue May 16, 2006 6:21 am    Post subject: Re: The name of constructors and destructors Reply with quote



In article <m94ag.9208$j7.305481 (AT) news (DOT) indigo.ie>,
NULL (AT) NULL (DOT) NULL ("Tom?s") wrote:

Quote:
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];

Quote:
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.


Quote:
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 "Smile" Liber <mailto:nevin (AT) eviloverlord (DOT) 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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.