Experimental reasons
Describes how to use the const description to protect class data from accidental modification.
Compiling the environment
VC6SP6 + win7x64
Project download
Copyconstruction_constmemberfunction.zip
Copy constructor error caused by using non-const member function
[CPP]View PlainCopy
- Class Cstudent
- {
- /// constant definition
- Public
- enum {Name_size_max = 64};
- /// structure, copy construction, destructor
- Public
- Cstudent ();
- Cstudent (const char* pcname, size_t nId = 1, int iage = 20);
- Cstudent (const cstudent& SRC);
- virtual ~cstudent ();
- /// member function
- Public
- void Clear ();
- Private
- void Init (); initialization of///< class
- void UnInit (); ///< class anti-initialization
- void copy (cstudent& src);
- // Setter, Getter
- Public
- //M_nid
- void Setter_m_nid (size_t nIn) {M_nid = NIn;}
- size_t Getter_m_nid () {return m_nid;}
- //M_cname
- void Setter_m_cname (const char* pcin)
- {
- if (NULL = = pcin)
- return;
- memset (M_cname, ' Name_size_max ');
- strncpy (M_cname, Pcin,
- (Strlen (Pcin) < (name_size_max-1))?
- Strlen (pcin): (name_size_max-1));
- }
- Const char* getter_m_cname () {return m_cname;}
- //M_iage
- void Setter_m_iage (int iIn) {m_iage = IIn;}
- int Getter_m_iage () {return m_iage;}
- /// member variable
- Private
- size_t M_nid; ///< School Number
- Char M_cname[name_size_max]; ///< name
- int m_iage; ///< Age
- };
[CPP]View PlainCopy
- Cstudent::cstudent (const cstudent& SRC)
- {
- Copy (SRC);
- }
- void Cstudent::copy (cstudent& src)
- {
- Setter_m_cname (Src.getter_m_cname ());
- Setter_m_nid (Src.getter_m_nid ());
- Setter_m_iage (Src.getter_m_iage ());
- }
The copy constructor is declared (const class&), but the copy constructor calls the non-const member function, which will give an error
Error C2664: ' Copy ': cannot convert parameter 1 from ' Const class Cstudent ' to ' Class Cstudent & '
If you want to check the error data, check the keywords.
Error C2664:cannot convert parameter 1 from ' Const class ' to ' Class & '
You need to change all the member functions called by the copy constructor (direct, simple) to the const member function.
You need to change the member function of the copy constructor call to class& or class* all to const class& or const class*
[CPP]View PlainCopy
- Class Cstudent
- {
- /// constant definition
- Public
- enum {Name_size_max = 64};
- /// structure, copy construction, destructor
- Public
- Cstudent ();
- Cstudent (const char* pcname, size_t nId = 1, int iage = 20);
- Cstudent (const cstudent& SRC);
- Cstudent (const cstudent* PSRC);
- virtual ~cstudent ();
- /// member function
- Public
- void Clear ();
- Private
- void Init (); initialization of///< class
- void UnInit (); ///< class anti-initialization
- void Copy (const cstudent* PSRC);
- // Setter, Getter
- Public
- //M_nid
- void Setter_m_nid (size_t nIn) {M_nid = NIn;}
- size_t Getter_m_nid () const {return M_nid;}
- //M_cname
- void Setter_m_cname (const char* pcin)
- {
- if (NULL = = pcin)
- return;
- memset (M_cname, ' Name_size_max ');
- strncpy (M_cname, Pcin,
- (Strlen (Pcin) < (name_size_max-1))?
- Strlen (pcin): (name_size_max-1));
- }
- Const char* getter_m_cname () const {return m_cname;}
- //M_iage
- void Setter_m_iage (int iIn) {m_iage = IIn;}
- int getter_m_iage () const {return m_iage;}
- /// member variable
- Private
- size_t M_nid; ///< School Number
- Char M_cname[name_size_max]; ///< name
- int m_iage; ///< Age
- };
[CPP]View PlainCopy
- Cstudent::cstudent (const cstudent& src) ///< conversion into const class&
- {
- Copy (&SRC);
- }
- Cstudent::cstudent (const cstudent* PSRC)
- {
- Copy (PSRC);
- }
- void Cstudent::copy (const cstudent* PSRC)
- {
- Setter_m_cname (Psrc->getter_m_cname ());
- Setter_m_nid (Psrc->getter_m_nid ());
- Setter_m_iage (Psrc->getter_m_iage ());
- }
The meaning of the const member function excerpt from the <<c++ const detailed >>
http://blog.csdn.net/zhuanshenweiliu/article/details/38223907
3. Const member functions
Any function that does not modify a data member (that is, a variable in a function) should be declared as a const type.
If you inadvertently modify the data member when you write the const member function, or if you call other non-const member functions, the compiler will indicate the error, which will undoubtedly improve the robustness of the program.
In the following program, the member function GetCount of a class stack is used only for counting, and logically getcount should be a const function. The compiler will indicate an error in the GetCount function.
Class Stack
{
Public
void Push (int elem);
int Pop (void);
int GetCount (void) const; Const member functions
Private
int m_num;
int m_data[100];
} ;
int Stack::getcount (void) const
{
+ + M_num; Compilation error, attempting to modify data member M_num
Pop (); Compile error, attempt to invoke non-const function
return m_num;
}
The declaration of the Const member function looks strange: The const keyword can only be placed at the end of a function declaration, presumably because other places are already occupied.
Some rules about the const function:
A. Const objects can access only const member functions, and non-const objects have access to arbitrary member functions, including const member functions.
B. The members of a const object are not modifiable, but objects maintained by a const object through pointers can be modified.
C. The const member function cannot modify the object's data, regardless of whether the object is of a const nature. It is checked at compile time, based on whether to modify member data.
http://blog.csdn.net/lostspeed/article/details/50291749
Copy constructors and const member functions