|
-
June 21st, 2011, 11:55 AM
#17
Re: need little help of bug fixers.
 Originally Posted by vkash
I don't understand what you say in first paragraph.
but if you want that catch should do nothing just keep it's original value then it is more simpler see it
Code:
// Overloaded assignment operator for CMessage objects
CMessage& operator=(const CMessage& aMess)
{
char arr[](pmessage); //will it work error may pointer to char conversion invalid
try
{
if(this == &aMess) // Check addresses, if equal
return *this; // return the 1st operand
// Release memory for 1st operand
delete[] pmessage;
pmessage = new char[strlen(aMess.pmessage) + 1];
// Copy 2nd operand string to 1st
strcpy_s(this->pmessage, strlen(aMess.pmessage) + 1, aMess.pmessage);
// Return a reference to 1st operand
return *this;
}
catch(bad_alloc &ex)
{
cout << "Memory allocation failed." << endl
<< "The information from the exception object is: "
<< ex.what() << endl;
cout<<"making no change\n";
pmessage=new char[strlen(arr)+1];
strcpy_s(pmessage,strlen(arr)+1),arr);
}
}
what about this.
One more time, in addition to Paul's comments, an = operator shouldn't contain any cout statements. Typically the user wouldn't want to see them or have any idea what to do about it. When something goes wrong, it should be up to the caller of that function to determine how to handle the error. All your function should do is indicate somehow to its caller that something has gone wrong, and provide a way for the caller to determine what. Exceptions are designed to provide just such a mechanism.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|