473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Which member functions do you provided for a class

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
5 1422
"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

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
3
by: lovecreatesbeauty | last post by:
Predefined class member functions and inheritance How many member functions on earth can be provided (predefined) by standard-compliant compilers? Scott Meyers says that there are 6:...
4
by: lovecreatesbeauty | last post by:
How many member functions will be predefined (provided by compilers implicitly) and called by standard-compliant compilers for a class? Does such a list be given in iso/iec 14882? Which member...
7
by: Gustavo L. Fabro | last post by:
Greetings! Some classes that once compiled without problems on VS 2003 have now problems on VS 2005 Beta 1. I'm talking about a __nogc class that is exported with __declspec(dllexport). The...
7
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0;...
4
by: tech | last post by:
Hi, I want to return a reference to member object provided in class Test, so have provided a getter function GetObj() and declared it const. However i'm getting a compiler error that i need to...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
5
by: Nitesh | last post by:
Objects of type X are basic building blocks of my application and there are a few functions which return XHandle (typedef X** XHandle). The task at hand needs to use a couple of these functions and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.