Click here to Skip to main content
16,003,319 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionSMTP,poxy -Hiding IP address-anybody have any idea? Pin
Hirosh24-Feb-02 20:15
Hirosh24-Feb-02 20:15 
GeneralShell or System Programing Pin
lx13024-Feb-02 19:29
lx13024-Feb-02 19:29 
GeneralRe: Shell or System Programing Pin
Michael Dunn24-Feb-02 19:38
sitebuilderMichael Dunn24-Feb-02 19:38 
GeneralRe: Shell or System Programing Pin
lx13026-Feb-02 17:04
lx13026-Feb-02 17:04 
Questionhow to create User DSN in ODBC & how to register .ocx file through install shield for vc++ 6 Pin
Chanda.com24-Feb-02 19:14
Chanda.com24-Feb-02 19:14 
AnswerRe: how to create User DSN in ODBC & how to register .ocx file through install shield for vc++ 6 Pin
Mazdak24-Feb-02 19:33
Mazdak24-Feb-02 19:33 
QuestionAre there some problems in the following source? Pin
Feng Qin24-Feb-02 19:04
Feng Qin24-Feb-02 19:04 
AnswerRe: Are there some problems in the following source? Pin
Paul M Watt24-Feb-02 19:33
mentorPaul M Watt24-Feb-02 19:33 
By calling:

<br />
string s1 = "the truth is out there";<br />


you are first invoking the default constructor of the string class, which you have not defined, so string::data is also undefined.

Next the operator= is invoked to assign "the truth is out there" to your object. This function is also undefined, therefore the default implementation is used and it will copy the pointer value that you sent in straight across. This is called a shallow copy, and it is generally bad.

If you want the code that you wrote to work, with out worrying about the shallow copy problem, you could make this change to the way that you invoke your objects:

<br />
string s1("the truth is out there";<br />


This will invoke the constructor that you have defined.

If you are going to have dynamically allocated data in any class, you should define these four functions, that will be automatically provided by the compiler if you do not choose to implement them yourself:

Default Constructor
string()
{
  data = NULL;
}


Destructor
string()
{
  delete[] data;
  data = NULL;
}


Copy Constructor
string(const string& rhs)
{
  //C: Verify that the rhs string exists.
  if (rhs.data)
  {
  //C: Get the length of the string.
    UINT nLen = strlen(rhs.data);
  //C: Allocate space for the data element.
    data = new char[nLen+1];
  //C: copy rhs into data.
    strcpy(data, rhs.data);
  }
  else
  {
  //C: rhs.data does not exist, therefore the string will go uninitialized.
    data = NULL;
  }
}


Finally...
Assignment operator
string &operator=(const string& rhs)
{
  //C: Check if rhs, is actually this object, ie. s1 = s1;
  if (this == &rhs)
  {
  //C: return a reference to this object.
  //   This will perpetuate things in C++ like a = b = c;
    return *this;
  }
  //C: Delete the old data associated with this object.
  //   Note, the code in this section is the same as the code in the destructor.
  delete[] data;

  //C: Note, the code in this section is the same as the code in the copy constructor.
  //   I usually put this common code in a private member function and share it between
  //   the two functions.
  //C: Verify that the rhs string exists.
  if (rhs.data)
  {
  //C: Get the length of the string.
    UINT nLen = strlen(rhs.data);
  //C: Allocate space for the data element.
    data = new char[nLen+1];
  //C: copy rhs into data.
    strcpy(data, rhs.data);
  }
  else
  {
  //C: rhs.data does not exist, therefore the string will go uninitialized.
    data = NULL;
  }

  //C: Finally, return a reference to this object.
  //   This will perpetuate things in C++ like a = b = c;
  return *this;
}



I have included a basic implementation of how it is done in order to set you onto the right track.
GeneralRe: Are there some problems in the following source? Pin
Joaquín M López Muñoz24-Feb-02 22:06
Joaquín M López Muñoz24-Feb-02 22:06 
GeneralRe: Are there some problems in the following source? Pin
Feng Qin24-Feb-02 23:28
Feng Qin24-Feb-02 23:28 
GeneralRe: Are there some problems in the following source? Pin
Paul M Watt24-Feb-02 23:40
mentorPaul M Watt24-Feb-02 23:40 
GeneralRe: Are there some problems in the following source? Pin
Joaquín M López Muñoz24-Feb-02 23:40
Joaquín M López Muñoz24-Feb-02 23:40 
GeneralRe: Are there some problems in the following source? Pin
Michael Dunn25-Feb-02 7:55
sitebuilderMichael Dunn25-Feb-02 7:55 
GeneralRe: Are there some problems in the following source? Pin
Joaquín M López Muñoz25-Feb-02 8:01
Joaquín M López Muñoz25-Feb-02 8:01 
GeneralRe: Are there some problems in the following source? Pin
Michael Dunn25-Feb-02 10:13
sitebuilderMichael Dunn25-Feb-02 10:13 
GeneralRe: Are there some problems in the following source? Pin
Joaquín M López Muñoz25-Feb-02 10:29
Joaquín M López Muñoz25-Feb-02 10:29 
GeneralRe: Are there some problems in the following source? Pin
Michael Dunn25-Feb-02 7:52
sitebuilderMichael Dunn25-Feb-02 7:52 
GeneralRe: Are there some problems in the following source? Pin
Joaquín M López Muñoz25-Feb-02 7:53
Joaquín M López Muñoz25-Feb-02 7:53 
QuestionWhat message should my app handle when user shutdown/logoff the system Pin
24-Feb-02 18:51
suss24-Feb-02 18:51 
AnswerRe: What message should my app handle when user shutdown/logoff the system Pin
Paul M Watt24-Feb-02 18:58
mentorPaul M Watt24-Feb-02 18:58 
GeneralRe: What message should my app handle when user shutdown/logoff the system Pin
24-Feb-02 19:04
suss24-Feb-02 19:04 
GeneralRe: What message should my app handle when user shutdown/logoff the system Pin
Paul M Watt24-Feb-02 19:17
mentorPaul M Watt24-Feb-02 19:17 
GeneralRe: Thanx Pin
24-Feb-02 21:10
suss24-Feb-02 21:10 
GeneralNetwork logins Pin
Coremn24-Feb-02 17:09
Coremn24-Feb-02 17:09 
GeneralRe: Network logins Pin
Jon Hulatt24-Feb-02 22:08
Jon Hulatt24-Feb-02 22:08 

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.