By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Which member functions do you provided for a class

P: n/a
Hello.
I often heard that four C++'s special members: default constructor,
destructor, copy constructor & assignment operator should be provided
for a class.

But in your real C++ project coding, when write a (general) class in
C++, which member functions will always be provided naturally by you in
your practice projects coding?

And does these "must" member functions selection procedure often called
class design?

(Sorry, I already post this topic at comp.lang.c++.moderated, but I'm
afraid that this naive and simple question won't be released by
moderator. I really desire your knowledge and experience on it, your
help will be appreciated and thanked greatly.)
Sincerely
lovecreatesbeauty

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"lovecreatesbeauty" <lo***************@gmail.com> wrote...
I often heard that four C++'s special members: default constructor,
destructor, copy constructor & assignment operator should be provided
for a class.

But in your real C++ project coding, when write a (general) class in
C++, which member functions will always be provided naturally by you in
your practice projects coding?
It depends on the use of the class. For example, if the class is going
to be derived from and used polymorphically, the destructor needs to be
virtual. Another often encountered situation is dynamically allocated
data member, read about "the rule of three" to see what's needed in that
case.
And does these "must" member functions selection procedure often called
class design?
Yes, I suppose so.
(Sorry, I already post this topic at comp.lang.c++.moderated, but I'm
afraid that this naive and simple question won't be released by
moderator.
I don't think your worries are warranted. Moderators can be slow to
pass the messages in, but a beginners question will never be refused
only based on the simplicity of the question.
I really desire your knowledge and experience on it, your
help will be appreciated and thanked greatly.)


A way to get quick help is to ask particular questions. By all means,
keep posting your queries, we'll do what we can.

V
Jul 23 '05 #2

P: n/a

lovecreatesbeauty wrote:
Hello.
I often heard that four C++'s special members: default constructor,
destructor, copy constructor & assignment operator should be provided
for a class.

But in your real C++ project coding, when write a (general) class in
C++, which member functions will always be provided naturally by you in your practice projects coding?


If your class is a value-semantic class (i.e., it represents a value,
such as a date or a calendar), you should implement a
default-constructor (required to use the object with STL containers), a
copy constructor, and a copy-assignment manipulator ('operator=').
Typically, it is useful to provide a free operator 'operator<<' as
well. In addition, you should provide a "set" manipulator and a "get"
accessor for each private data member (although you may choose to
implement a more insulating interface for container members than
returning the container itself).

If you class is not value-semantic (i.e., it represents a mechanism
such as a state machine or a memory pool), you should explicitly
disallow copy construction and copy assignment. You may choose to
provide a default constructor if it makes sense.

Additionally, as pointed out else-thread, you must at least declare the
destructor virtual if the class is meant to be used polymorphically
(i.e., as a base class).

HTH, /david

Jul 23 '05 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:3s********************@comcast.com...
And does these "must" member functions selection procedure often called
class design?


Yes, I suppose so.

Victor,
You actually understood that question? :-)
-H

Jul 23 '05 #4

P: n/a
Howard wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:3s********************@comcast.com...

And does these "must" member functions selection procedure often called
class design?


Yes, I suppose so.


Victor,
You actually understood that question? :-)


Of course, I guess... :-) It's a non-native English speaker thing.
Jul 23 '05 #5

P: n/a
The question I asked is equal to this question - "What is class design"
or "Class design includes whether one special member function (default
constructor, destructor, copy constructor, assignment operator... ) is
selected to be overridden."

I'm a Chinese, very sorry for my poor English. I really thank Victor
and David, thanks.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.