Copy constructors and const member functions

Source: Internet
Author: User
Tags constant definition

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
  1. Class Cstudent
  2. {
  3. /// constant definition
  4. Public
  5. enum {Name_size_max = 64};
  6. /// structure, copy construction, destructor
  7. Public
  8. Cstudent ();
  9. Cstudent (const char* pcname, size_t nId = 1, int iage = 20);
  10. Cstudent (const cstudent& SRC);
  11. virtual ~cstudent ();
  12. /// member function
  13. Public
  14. void Clear ();
  15. Private
  16. void Init (); initialization of///< class
  17. void UnInit (); ///< class anti-initialization
  18. void copy (cstudent& src);
  19. // Setter, Getter
  20. Public
  21. //M_nid
  22. void Setter_m_nid (size_t nIn) {M_nid = NIn;}
  23. size_t Getter_m_nid () {return m_nid;}
  24. //M_cname
  25. void Setter_m_cname (const char* pcin)
  26. {
  27. if (NULL = = pcin)
  28. return;
  29. memset (M_cname, ' Name_size_max ');
  30. strncpy (M_cname, Pcin,
  31. (Strlen (Pcin) < (name_size_max-1))?
  32. Strlen (pcin): (name_size_max-1));
  33. }
  34. Const char* getter_m_cname () {return m_cname;}
  35. //M_iage
  36. void Setter_m_iage (int iIn) {m_iage = IIn;}
  37. int Getter_m_iage () {return m_iage;}
  38. /// member variable
  39. Private
  40. size_t M_nid; ///< School Number
  41. Char M_cname[name_size_max]; ///< name
  42. int m_iage; ///< Age
  43. };

[CPP]View PlainCopy
    1. Cstudent::cstudent (const cstudent& SRC)
    2. {
    3. Copy (SRC);
    4. }
    5. void Cstudent::copy (cstudent& src)
    6. {
    7. Setter_m_cname (Src.getter_m_cname ());
    8. Setter_m_nid (Src.getter_m_nid ());
    9. Setter_m_iage (Src.getter_m_iage ());
    10. }

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
  1. Class Cstudent
  2. {
  3. /// constant definition
  4. Public
  5. enum {Name_size_max = 64};
  6. /// structure, copy construction, destructor
  7. Public
  8. Cstudent ();
  9. Cstudent (const char* pcname, size_t nId = 1, int iage = 20);
  10. Cstudent (const cstudent& SRC);
  11. Cstudent (const cstudent* PSRC);
  12. virtual ~cstudent ();
  13. /// member function
  14. Public
  15. void Clear ();
  16. Private
  17. void Init (); initialization of///< class
  18. void UnInit (); ///< class anti-initialization
  19. void Copy (const cstudent* PSRC);
  20. // Setter, Getter
  21. Public
  22. //M_nid
  23. void Setter_m_nid (size_t nIn) {M_nid = NIn;}
  24. size_t Getter_m_nid () const {return M_nid;}
  25. //M_cname
  26. void Setter_m_cname (const char* pcin)
  27. {
  28. if (NULL = = pcin)
  29. return;
  30. memset (M_cname, ' Name_size_max ');
  31. strncpy (M_cname, Pcin,
  32. (Strlen (Pcin) < (name_size_max-1))?
  33. Strlen (pcin): (name_size_max-1));
  34. }
  35. Const char* getter_m_cname () const {return m_cname;}
  36. //M_iage
  37. void Setter_m_iage (int iIn) {m_iage = IIn;}
  38. int getter_m_iage () const {return m_iage;}
  39. /// member variable
  40. Private
  41. size_t M_nid; ///< School Number
  42. Char M_cname[name_size_max]; ///< name
  43. int m_iage; ///< Age
  44. };

[CPP]View PlainCopy
  1. Cstudent::cstudent (const cstudent& src) ///< conversion into const class&
  2. {
  3. Copy (&SRC);
  4. }
  5. Cstudent::cstudent (const cstudent* PSRC)
  6. {
  7. Copy (PSRC);
  8. }
  9. void Cstudent::copy (const cstudent* PSRC)
  10. {
  11. Setter_m_cname (Psrc->getter_m_cname ());
  12. Setter_m_nid (Psrc->getter_m_nid ());
  13. Setter_m_iage (Psrc->getter_m_iage ());
  14. }



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.