C ++ replication Control

Source: Internet
Author: User

From: http://longsy.javaeye.com/blog/353316

Author: longsy

 

When defining a new type, you can use the copy constructor, value assignment operator, and destructor to display or implicitly copy, assign, and revoke objects.

1. copy constructor: A constructor that only has a single parameter and is a reference to an object of this type (usually const modified). It is called ~
The copy function can be used:
1) display or implicitly Initialize an object of the same type.
2) copy an object and pass it as a real parameter to a function.
3) copy an object when returning from the function
4) initialize elements in the sequential container
5) initialize array elements based on the element initialization list
A. object initialization form
Direct initialization: usually place the initialization formula in parentheses
Copy initialization: Use the = symbol
String STR = "123"; // copy-initialization
String str1 = STR; // copy-initialization
B. Participate in the return value
When the form parameter or return value is of the class type, the copy constructor replicates the data.
String make_copy (size_t, const string &, const string &);
C. initialize container elements
Vector <string> sevc (5 );
Use the string default constructor to create a temporary value to initialize SVEC, and then use the copy constructor to copy the temporary value to each element of sevc.
2. merged copy constructor
Initialize members one by one to initialize the new object as a copy of the original object.
3. define your own copy constructor
Myclass (const myclass & Mc ){//...}
The replication constructor is not set to explicit.
4. Prohibit Replication
The Declaration displayed by the copy constructor is private.

5. Value assignment operator: The value assignment operator function is called when an object of the class type has been initialized.
Myclass & operator = (const myclass &) {//... return * This}
6. Value assignment operator for merging
Assign values to members one by one
Generally, the value assignment operator is used with the copy constructor.

7. destructor: cancels class objects and automatically calls
Rule 3: If the class requires destructor, it also needs to copy the constructor and the value assignment operator.
~ MyClass (){//...}
8. Synthesize destructor: Undo non-static objects according to the declared reverse order,Objects pointed to by pointer members cannot be deleted.

Example:
Header file, rectangle. h

C ++ code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://longsy.javaeye.com/blog/353316# ">
  1. /*
  2. Language: c ++
  3. Author: longsy
  4. File: rectangle. h
  5. */
  6. NamespaceLongsy {
  7. ClassRectangle {
  8. IntWidth;
  9. IntHeight;
  10. Public:
  11. // Default constructor with default real parameters
  12. Rectangle (IntW = 0,IntH = 0 );
  13. // Copy the constructor
  14. Rectangle (ConstRectangle & rect );
  15. // Value assignment operator
  16. Rectangle & operator = (ConstRectangle & rect );
  17. // Destructor
  18. ~ Rectangle ();
  19. VoidDraw ();
  20. };
  21. }
/* Language: c ++ author: longsyfile: rectangle. h */namespace longsy {class Rectangle {int width; int height; public: // default constructor with default real parameters Rectangle (int w = 0, int h = 0 ); // copy the constructor Rectangle (const Rectangle & rect); // value assignment operator Rectangle & operator = (const Rectangle & rect); // analyze constructor ~ Rectangle (); void Draw ();};}

Implementation file, rectangle. cpp

C ++ code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://longsy.javaeye.com/blog/353316# ">
  1. /*
  2. Language: c ++
  3. Author: longsy
  4. File: rectangle. cpp
  5. */
  6. # Include "rectangle. h"
  7. Using NamespaceLongsy;
  8. # Include <iostream>
  9. Rectangle: Rectangle (IntW,IntH): width (w), height (h)
  10. {
  11. Std: cout <"Rectangle (int w, int h)" <std: endl;
  12. }
  13. Rectangle: Rectangle (ConstRectangle & rect)
  14. {
  15. This-> Width = rect. width;
  16. This-> Height = rect. height;
  17. Std: cout <"Rectangle (const Rectangle & rect)" <std: endl;
  18. }
  19. Rectangle & Rectangle: operator = (ConstRectangle & rect)
  20. {
  21. This-> Width = rect. width;
  22. This-> Height = rect. height;
  23. Std: cout <"operator = (const Rectangle & rect)" <std: endl;
  24. Return*This;
  25. }
  26. Rectangle ::~ Rectangle ()
  27. {
  28. This-> Width = 0;
  29. This-> Height = 0;
  30. Std: cout <"~ Rectangle () "<std: endl;
  31. }
  32. VoidRectangle: Draw ()
  33. {
  34. Std: cout <"Draw the rectangle, width:" <This-> Width/
  35. <", Height:"
  36. }
/*language:c++author:longsyfile:rectangle.cpp*/#include "rectangle.h"using namespace longsy;#include <iostream>Rectangle::Rectangle(int w,int h) : width(w),height(h) {std::cout << "Rectangle(int w,int h)" << std::endl;}Rectangle::Rectangle(const Rectangle &rect) {this->width = rect.width;this->height = rect.height;std::cout << "Rectangle(const Rectangle &rect)" <<std::endl;}Rectangle& Rectangle::operator=(const Rectangle &rect){this->width = rect.width;this->height = rect.height;std::cout << "operator=(const Rectangle &rect)" << std::endl;return *this;}Rectangle::~Rectangle(){this->width = 0;this->height = 0;std::cout << "~Rectangle()" << std::endl;}void Rectangle::Draw(){std::cout << "Draw the rectangle,width:" << this->width /<< ",height:" << height <<std::endl;}

Test file, test. cpp

C ++ code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://longsy.javaeye.com/blog/353316# ">
  1. /*
  2. Language: c ++
  3. Author: longsy
  4. File: test. cpp
  5. */
  6. # Include "rectangle. h"
  7. Using NamespaceLongsy;
  8. IntMain ()
  9. {
  10. Rectangle rect; // call the default constructor.
  11. Rect. Draw ();
  12. Rectangle rect1 (1, 1); // Rectangle (int w, int h)
  13. Rect1.Draw ();
  14. Rectangle rect2 = rect1; // call the copy constructor.
  15. Rect2.Draw ();
  16. Rectangle rect3 (2, 2); // Rectangle (int w, int h)
  17. Rect3.Draw ();
  18. Rect3 = rect1; // call the value assignment operator
  19. Rect3.Draw ();
  20. Return(0); // call the destructor
  21. }
  22. /*
  23. Running result:
  24. Rectangle (int w, int h)
  25. Draw the rectangle, width: 0, height: 0
  26. Rectangle (int w, int h)
  27. Draw the rectangle, width: 1, height: 1
  28. Rectangle (const Rectangle & rect)
  29. Draw the rectangle, width: 1, height: 1
  30. Rectangle (int w, int h)
  31. Draw the rectangle, width: 2, height: 2
  32. Operator = (const rectangle & rect)
  33. Draw the rectangle, width: 1, height: 1
  34. ~ Rectangle ()
  35. ~ Rectangle ()
  36. ~ Rectangle ()
  37. ~ Rectangle ()
  38. */
/* Language: c ++ author: longsyfile: test. cpp */# include "rectangle. h "using namespace longsy; int main () {Rectangle rect; // call the default constructor rect. draw (); Rectangle rect1 (); // Rectangle (int w, int h) rect1.Draw (); Rectangle rect2 = rect1; // call the copy constructor rect2.Draw (); rectangle rect3 (2, 2); // Rectangle (int w, int h) rect3.Draw (); rect3 = rect1; // call the value assignment operator rect3.Draw (); return (0 ); // call the Destructor}/* running result: Rectangle (int w, int h) Draw the rect Angle, width: 0, height: 0 Rectangle (int w, int h) Draw the rectangle, width: 1, height: 1 Rectangle (const Rectangle & rect) Draw the rectangle, width: 1, height: 1 Rectangle (int w, int h) Draw the rectangle, width: 2, height: 2 operator = (const Rectangle & rect) Draw the rectangle, width: 1, height: 1 ~ Rectangle ()~ Rectangle ()~ Rectangle ()~ Rectangle ()*/

Related Article

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.