Click here to Skip to main content
15,914,488 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Drawing caption in dialog, who can help me? Pin
Paul M Watt16-Mar-02 6:35
mentorPaul M Watt16-Mar-02 6:35 
GeneralRe: Drawing caption in dialog, who can help me? Pin
generic_user_id16-Mar-02 8:10
generic_user_id16-Mar-02 8:10 
GeneralRe: Drawing caption in dialog, who can help me? Pin
Paul M Watt16-Mar-02 11:13
mentorPaul M Watt16-Mar-02 11:13 
QuestionHow to make the menu of XP's style ? Pin
mzt16-Mar-02 3:54
mzt16-Mar-02 3:54 
AnswerRe: How to make the menu of XP's style ? Pin
16-Mar-02 4:55
suss16-Mar-02 4:55 
QuestionHow to make the sensitive effect of mouse of CTabCtrl's push-button ? Pin
mzt16-Mar-02 3:49
mzt16-Mar-02 3:49 
GeneralCopy Constructors & operator= when using pointers Pin
John Cruz16-Mar-02 3:13
John Cruz16-Mar-02 3:13 
GeneralRe: Copy Constructors & operator= when using pointers Pin
Paul M Watt16-Mar-02 6:30
mentorPaul M Watt16-Mar-02 6:30 
Example class:

class array
{
private:
  int  n_MaxLength;
  int  n_array;

  bool Clone   (const array& rhs);
  bool Destroy ();
public:
  array ();
  ~array ();

  array (const array& rhs);
  array& operator=(const array& rhs);

  ...
  //Other functions like setmaxlength, setItem and so on.
};


Here is the constructor and destructor:
array ();
{
  // start out with an initial size of 10
  n_MaxLength = 10;
  n_array = new int[10];
}

~array ();
{
   delete[] n_array;
   n_array = NULL;
}


Here is the copy constructor:
array (const array& rhs)
{
  n_MaxLength = rhs.n_MaxLength;
  //Allocate space for the dynamic array.
  n_array = new int[n_MaxLength];
  //Verify that the memory was properly allocated
  if (NULL != n_array)
  {
  //Copy the contents from rhs into this array.
    memcpy(n_array, rhs.n_array, sizeof(int) * n_MaxLength);
  }
}


Here is the assignment operator:
  // the assignment operator always returns a reference to this object (*this) in order
  // to allow operations like this to succeed in C++:  a = b = c = d;
  // Otherwise only this opertion would succeed a = b;
  array& operator= (const array& rhs)
  {
    //Check that rhs is not = to this.  If it is simply return.  There is no 
    //sense in trying to copy over ourselves.
    if (this == &rhs)
    {
      return *this;
    }
    //The test above was performed, because this code will delete the current
    //memory in this object.  If we are copying to ourselves, the memory will have been
    //deleted, and the new values that we copy to ourselves will be garbage.
//Delete Code
    delete[] n_array;
    n_array = NULL;
//End Delete Code

//Copy Code
    //Copy the new values
    n_MaxLength = rhs.n_MaxLength;
    //Allocate space for the dynamic array.
    n_array = new int[n_MaxLength];
    //Verify that the memory was properly allocated
    if (NULL != n_array)
    {
    //Copy the contents from rhs into this array.
      memcpy(n_array, rhs.n_array, sizeof(int) * n_MaxLength);
    }
//End Copy Code
    //Return a reference to this pointer.
    return *this;
  }


Notice that the Delete code in the assignment operator is the same as the delete code in the destructor. And also that the copy code in the copy constructor is the same as the copy code in the assignment operator. When my class get larger, and there are alot of variables to manage, I usually organize my classes like this to help reduce redundant code:

//Destroy private function.
void Destroy()
{
   delete[] n_array;
   n_array = NULL;
}
//Clone, private function
void Clone()
{
  //Copy the new values
  n_MaxLength = rhs.n_MaxLength;
  //Allocate space for the dynamic array.
  n_array = new int[n_MaxLength];
  //Verify that the memory was properly allocated
  if (NULL != n_array)
  {
  //Copy the contents from rhs into this array.
    memcpy(n_array, rhs.n_array, sizeof(int) * n_MaxLength);
  }
}

//Destructor
~array ();
{
   Destroy ();
}

//Copy constructor
array (const array& rhs)
{
  Clone();
}

//assignment operator
array& operator= (const array& rhs)
{
  //Check that rhs is not = to this.  If it is simply return.  There is no
  //sense in trying to copy over ourselves.
  if (this == &rhs)
  {
    return *this;
  }
  //Delete dynamic memory.
  Destroy();

  //Copy rhs into this.
  Clone();

  //Return a reference to this pointer.
  return *this;
}

GeneralSDK problem! Pin
hph16-Mar-02 3:13
hph16-Mar-02 3:13 
GeneralRe: SDK problem! Pin
Nish Nishant16-Mar-02 3:17
sitebuilderNish Nishant16-Mar-02 3:17 
GeneralRe: SDK problem! Pin
Peter Liddle16-Mar-02 9:25
Peter Liddle16-Mar-02 9:25 
GeneralSimple question Pin
John Cruz16-Mar-02 1:41
John Cruz16-Mar-02 1:41 
GeneralRe: Simple question Pin
Stan Shannon16-Mar-02 1:49
Stan Shannon16-Mar-02 1:49 
GeneralRe: Simple question Pin
John Cruz16-Mar-02 2:05
John Cruz16-Mar-02 2:05 
GeneralRe: Simple question Pin
Stan Shannon16-Mar-02 2:27
Stan Shannon16-Mar-02 2:27 
GeneralRe: Simple question Pin
John Cruz16-Mar-02 2:29
John Cruz16-Mar-02 2:29 
GeneralRe: Simple question Pin
CDuddley16-Mar-02 13:24
CDuddley16-Mar-02 13:24 
GeneralRe: Simple question Pin
John Cruz16-Mar-02 14:51
John Cruz16-Mar-02 14:51 
GeneralFocus Pin
meirav15-Mar-02 23:36
meirav15-Mar-02 23:36 
GeneralRe: Focus Pin
Mazdak16-Mar-02 0:01
Mazdak16-Mar-02 0:01 
GeneralRe: Focus Pin
Nish Nishant16-Mar-02 1:02
sitebuilderNish Nishant16-Mar-02 1:02 
GeneralVC++ 6.0 string bug ? (take a look plz) Pin
Lockhart15-Mar-02 22:43
Lockhart15-Mar-02 22:43 
GeneralRe: VC++ 6.0 string bug ? (take a look plz) Pin
Christian Graus15-Mar-02 22:53
protectorChristian Graus15-Mar-02 22:53 
GeneralRe: VC++ 6.0 string bug ? (take a look plz) Pin
Lockhart15-Mar-02 23:50
Lockhart15-Mar-02 23:50 
GeneralRe: VC++ 6.0 string bug ? (take a look plz) Pin
Christian Graus15-Mar-02 23:59
protectorChristian Graus15-Mar-02 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.