-
February 20th, 2007, 02:43 PM
#1
operator overload problem
I make a "+" overload for my class as what CString does but found it cannot give the right result.
Here is my code:
CmyClass& operator +(CmyClass& a, CmyClass& b)
{
CmyClass c(...);
// assign a+b value to c
return c;
}
and then if i run
CmyClass a, b, c;
c = a+b;
give a wrong c result;
I have thought about c is supposed to have only local lifetime, but CString does the same
this is what CString does:
CString AFXAPI operator+(const CString& string1, const CString& string2)
{
CString s;
s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pchData, string2.GetData()->nDataLength, string2.m_pchData);
return s;
}
-
February 20th, 2007, 02:49 PM
#2
Re: operator overload problem
You return local object by reference, what is wrong. CString returns local object by value (so it's returning a copy of local object), what is OK. You have to change return valuefrom CMyClass& to CMyClass.
Cheers,
Hob
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 20th, 2007, 03:06 PM
#3
Re: operator overload problem
And for const correctness, CmyClass should be passed as const reference.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 20th, 2007, 03:20 PM
#4
Re: operator overload problem
Hobson,
Thank you for your help. Actually that is a typo. But i do have it solved by your words of "copy object". I forgot to make a copy construct function. Thank you!
-
February 20th, 2007, 03:40 PM
#5
Re: operator overload problem
Hi,
Once i add const prefix, i got an error below while compiling.
error C2662: 'Func' : cannot convert 'this' pointer from 'const class CMyClass' to 'class CMyClass &'
in Func, i return a class member;
do you know what the problem is? Thank you!
Originally Posted by SuperKoko
And for const correctness, CmyClass should be passed as const reference.
-
February 20th, 2007, 03:49 PM
#6
Re: operator overload problem
I got it. i forget to use const member funcion in the class with const reference.
sorry to bother. I haven't used c++ for a while, some basics just slip out of my mind.
Originally Posted by olin
Hi,
Once i add const prefix, i got an error below while compiling.
error C2662: 'Func' : cannot convert 'this' pointer from 'const class CMyClass' to 'class CMyClass &'
in Func, i return a class member;
do you know what the problem is? Thank you!
-
February 21st, 2007, 11:11 AM
#7
Re: operator overload problem
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
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
|