مهندس موفق الکترونیک

تابع سازنده کپی در ++C

سازنده کپی (Copy Constructor)، سازنده‌ای است که شی جدید را با استفاده از یک شی از همان کلاس که قبلاً ساخته شده است آغاز می‌کند. سازنده کپی برای:

  • آغاز یک شی از شی دیگر از همان نوع
  • کپی یک شی برای ارسال آن به عنوان آرگومانی از تابع
  • کپی یک شی برای بازگرداندن آن از یک تابع

استفاده می‌شود. اگر برای یک کلاس سازنده کپی تعریف نشود، کامپایلر خودش برای آن کلاس سازنده کپی تعریف می‌کند. اگر یک کلاس دارای متغیرهای اشاره‌گر و اختصاص پویای حافظه باشد، آنگاه حتماً باید یک سازنده کپی داشته باشد. متداول‌ترین شکل سازنده کپی به صورت زیر است.

classname (const classname &obj) {
   // body of constructor
}

در این عبارت obj یک رفرنس به شی‌ای است که برای آغاز شی دیگر به کار می‌رود.

#include <iostream>

using namespace std;

class Line {

   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len) {
   cout << "Normal constructor allocating ptr" << endl;
   
   // allocate memory for the pointer;
   ptr = new int;
   *ptr = len;
}

Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = new int;
   *ptr = *obj.ptr; // copy the value
}

Line::~Line(void) {
   cout << "Freeing memory!" << endl;
   delete ptr;
}

int Line::getLength( void ) {
   return *ptr;
}

void display(Line obj) {
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main() {
   Line line(10);

   display(line);

   return 0;
}

با اجرای کد فوق، خروجی زیر نتیجه می‌شود.

Normal constructor allocating ptr
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!

حال اجازه دهید همان مثال را با اندکی تغییر، برای ساختن شی دیگر با استفاده از یک شی از پیش موجود به کار ببندیم.

#include <iostream>

using namespace std;

class Line {
   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len) {
   cout << "Normal constructor allocating ptr" << endl;
   
   // allocate memory for the pointer;
   ptr = new int;
   *ptr = len;
}

Line::Line(const Line &obj) {
   cout << "Copy constructor allocating ptr." << endl;
   ptr = new int;
   *ptr = *obj.ptr; // copy the value
}

Line::~Line(void) {
   cout << "Freeing memory!" << endl;
   delete ptr;
}

int Line::getLength( void ) {
   return *ptr;
}

void display(Line obj) {
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main() {

   Line line1(10);

   Line line2 = line1; // This also calls copy constructor

   display(line1);
   display(line2);

   return 0;
}

با اجرای این کد، خروجی زیر حاصل می‌شود.

Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Copy constructor allocating ptr.
Length of line : 10
Freeing memory!
Freeing memory!
Freeing memory!

منبع: ترجمه از سایت tutorialspoint.com

مطلب پیشنهادی:  اورلود کردن عملگرهای ورودی/خروجی در ++C

در جلسه بعدی آموزش ++C  با ما همراه باشید.

اگر این نوشته‌ برایتان مفید بود لطفا کامنت بنویسید.

مطالعه دیگر جلسات این آموزش<< جلسه قبلی                    جلسه بعدی >>

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *