Which member functions do you provided for a class

L

lovecreatesbeauty

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
 
V

Victor Bazarov

lovecreatesbeauty said:
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
 
D

davidrubin

lovecreatesbeauty said:
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
 
L

lovecreatesbeauty

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top