CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58

    Do i need a copy constructor?

    Good day to all! i just have a question regarding copy constructor... i created some plain class which i just based from an example of CToken Class that never used a copy constructor... is copy constructor really required in a class? can anyone give me a good explaination about copy constructor when to be used or not... here is some of my class that i created:

    #include "CFunctionsLib.h"
    #include "VRS.h"



    class CItem
    {
    public:
    int GetItemAvailable(CString sItemCode);
    CString GetItemName(CString sItemCode);
    CString m_sInv_Type;
    BOOL CheckItemExist(CString sItem);
    void AddItem(CString sItemCode, int iNoItems);
    BOOL DeductItem(int iNoItems);
    BOOL GetItem();
    CString m_sItemType, m_sItemCode,
    m_sItemPrevCode, m_sItemCategory,
    m_sUserID, m_sItemDatePurchased,

    m_sItemTitle, m_sItemDirector, m_sItemStarArtist,
    m_sItemFilmMaker, m_sRentMaxDate;

    double dItemPurchased, dItemRentAmt,
    dItemSalesAmt, dItemPenalty, dPriceDue;
    int iRentDuration, iSalesDuration, iNoOfItems, iItemAvailable;

    void Initialize();
    void LoadItem();
    CItem(); // Constructor
    virtual ~CItem(); // Destructor


    private:
    CFunctionsLib cFuncs;
    CVRSApp *pApp;
    _RecordsetPtr pSet;
    _CommandPtr pCmd;
    CString sSelect;
    };

    This example is about the Item Object (could be anything like apple, VCD, etc...).
    Sorry for being a dumb in C++... Thanks for any reply you made... More power!

  2. #2
    Join Date
    Feb 2004
    Location
    Philippines
    Posts
    58

    Re: Do i need a copy constructor?

    my additional codes are here:
    //Cpp file of CItem Class

    CItem::CItem()
    {
    Initialize();
    }

    CItem::~CItem()
    {
    }


    void CItem::Initialize()
    {
    pApp = (CVRSApp*)AfxGetApp();
    sSelect = "";
    m_sItemType = "";
    m_sItemCode = "";
    m_sItemPrevCode = "";
    m_sItemCategory = "";
    m_sUserID = "";
    m_sItemDatePurchased = "";
    m_sRentMaxDate = "";

    dItemPurchased = 0;
    dItemRentAmt = 0;
    dItemSalesAmt = 0;
    dItemPenalty = 0;

    iItemAvailable = 0;
    iRentDuration = 0;
    iSalesDuration = 0;
    iNoOfItems = 0;
    }

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Do i need a copy constructor?

    Copy constructor is needed if you require your class instance to be copyable like the code below. If it is not required to be copyable, you can declare it private and leave out the definition.

    Code:
    MyClass a;
    MyClass b(a);   // Using copy constructor to create instance b.
    MyClass c = a;    // Same as above
    In the situation that you really need it to be copyable, the default copy constructor provided by your compiler is sufficient if all your member variables are POD, like int, char, etc. However, if your class has pointer that requires to dynamically de/allocated memory, then you have to write your own copy constructor.

    In addition, if you have written a copy constructor, normally you also need to provide an assignment operator, i.e. operator=().

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Do i need a copy constructor?

    You should check if the default copy constructor operates well. It does so, if every field of your class can be correctly and sepatarely copied using its copy constructor. For example, every field that is a class should have a proper copy constructor. If your class has some links (maybe withing some fields that are classes) that are pointing between fields, the separate copying is unable to proceed. If you have a link as a field, it may be a link not to shared resource, and this private resource would not be copied. Check these points and if there is not such a compications, use default copy constructor (i.e. do not write your own). Otherwise write...
    "Programs must be written for people to read, and only incidentally for machines to execute."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured