|
-
June 21st, 2011, 11:55 AM
#31
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.
-
June 21st, 2011, 12:04 PM
#32
Re: need little help of bug fixers.
And to add to what GCDEF mentions, if you wanted a catch block, the only viable thing to do is rethrow the std::bad_alloc back to the caller.
You shouldn't be attempting to create more dynamic strings (this is what caused the exception to begin with), and you shouldn't be issuing cout statements or calling exit(0).
As to the exit(0), to emphasize what was already stated -- what if I need to close files, handles, shut down a socket, or some other cleanup routine, but I can't because your CMessage object stops the application dead by calling exit(0)? You would receive huge complaints from persons using your class.
Regards,
Paul McKenzie
-
June 21st, 2011, 12:31 PM
#33
Re: need little help of bug fixers.
thanks to paul and GCDEF for giving me information about exception and how to use them.
BUT I have still problem when data is returned by reference I have studied both return by reference and return by value(a month ago) usually return by value is used. Can you please refresh my memory on what is difference in these two returning methods.
-
June 21st, 2011, 12:36 PM
#34
Re: need little help of bug fixers.
 Originally Posted by vkash
thanks to paul and GCDEF for giving me information about exception and how to use them.
BUT I have still problem when data is returned by reference I have studied both return by reference and return by value(a month ago) usually return by value is used. Can you please refresh my memory on what is difference in these two returning methods.
If you return a reference to something, it has to exist after the function exits. That's why you can't return a reference to a variable declared in the function because it goes out of scope when the function ends.
Paul explained it in post 18
http://www.codeguru.com/forum/showpo...9&postcount=18
-
June 21st, 2011, 12:37 PM
#35
Re: need little help of bug fixers.-> what about this.
 Originally Posted by vkash
moral never return any data by any way which is somehow related to data inside function braces.
Is my explanation correct.
The term 'data' is not accurate in this discussion. You should talk about 'objects', because objects have a lifetime associated with them (they are created and destroyed at some time during program execution), whereas data has not (it may linger in memory). A pointer or reference to an object has no knowledge of the object's lifetime. Thus you can have a reference to an object that no longer exists.
 Originally Posted by vkash
but i am still in little confusion
firstly=> for the above what happen when i write (motto1=motto2)=motto3 this (motto1=motto2) is called first the returning data is reference to what?? even now operator= will called once again with returned data motto3. In this returned data( after call motto1 and motto2) is used to call once again is it existing even now if yes then what is it's scope and when it will destructed. In the book it is written if we return here by value (in first call of motto1 and motto2) then there will an error. I think error should in both case but it is not happening in case of reference.(even after all these. this code is working is fine)
When you implement an overloaded operator, a copy constructor or an assignment operator, you should implement them in such a way that they work in a similar way as the same operations applied to built-in types. This is not mandated by the standard, but if you deviate from this, your code will be hard to use, because it has subtle differences from what the average programmer (and therefore other code) would expect. As an example of how important this is: std::auto_ptr's copy constructor and assignment operator do not behave like other types, which has caused it to become deprecated in the new standard (also, because there is a better alternative now).
What this means in practice is that your overloaded operators should use a specific prototype, e.g. the binary + operator should return a copy, an assignment operator should return a reference to itself, etc. In code:
Code:
class X
{
public:
X(const X&);
X& operator =(const X&);
X& operator +=(const X&);
X operator +(const X&) const; // or you can define this as a non-member function taking two const X&'s.
};
So, to understand the code you posted, you should think about what this code does:
Code:
int x = 1, y = 2, z = 3;
(x = y) = z;
What is the value of x, y and z after the second statement?
When you use your own class instead of an int, the behavior should be the same. So, then the question becomes: how should the assignment operator of your own class be implemented to achieve that?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 21st, 2011, 01:12 PM
#36
Re: need little help of bug fixers.-> what about this.
I understood it
at the end of operator+ a variable X of type is returned that is destroyed latter but returning by reference work in case of operator= because here a reference to object which is transfered to it at calling not created in operator=. that's thing i want to know. thanks guys.
One more thing is (x=y)=z as stated by D_Drmmr here if x y z are objects then x=y is done first now x and y are same. a reference to x is returned then x = z called thus all happen correctly.(now all three are equal) that's why operator= works without hang in both code.
BIG thanks to all persons for helping now i think my problems is solved.
-
June 21st, 2011, 03:03 PM
#37
Re: need little help of bug fixers.-> what about this.
 Originally Posted by vkash
One more thing is (x=y)=z as stated by D_Drmmr here if x y z are objects then x=y is done first now x and y are same. a reference to x is returned then x = z called thus all happen correctly.(now all three are equal) that's why operator= works without hang in both code.
I suggest you learn to check your results. Your understanding seems to be in the right direction, but your conclusion is wrong. If you take the time to write a small program and check the result, you'll have a lot smaller chance of misunderstanding.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
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
|