|
-
April 28th, 2004, 06:19 PM
#1
Overloaded operators calling each other.
Hello peoples.
I'm just learning about C++ classes, and overloading operators. I'm having some trouble with two overloads, one of which is operator==(char * ), and the other one is operator==(const CStr&).
Both of these methods contain the exact same code, with the only difference is that one takes a pointer to a character array, while the other one takes a class instance. I would like to be able to call the operator== that takes the character array from within the class instance method, to cut down on code.
Class Calls (Both of which are public):
int operator==( char *);
int operator==(const CStr&);
Method for == "String", same code for Class instance:
int CStr::operator ==(char * pIncomingString)
{
int Match = 1;
int Counter = 0;
while (Counter < Next && Match != 0)
{
if (*(pIncomingString + Counter) == Text[Counter++])
Match = 1;
else
Match = 0;
}
return Match;
}
From within the method with the class instance, I have tried a variety of lines to try to call this, but so far either it doesn't work at all, or it errors out.
operator==(IncomingClass.Text);
I have been working with this line, but no luck. I've tried to do Text==(IncomingClass.Text). This line will compile fine, but does not give me the expected results.
This code is for a project, which is complete, but am curious why I cannot call correctly another method and reduce duplicate code.
Thanks
-Jeff
-
April 28th, 2004, 07:32 PM
#2
If you have already implemented the operator == char* for your class (Looks like you have), and the Text member-variable contains the actual c-string:
Code:
bool CStr::operator ==(const CStr& rhs)
{
return (*this) == rhs.Text;
}
You should also consider using std::string instead of making your own string-class. It's there, so why not use it?
-
April 28th, 2004, 08:29 PM
#3
We were forbidden from using any string functions for this assignment. If we wanted to use them, we had to make them. I have tried your suggestion, but unfortunately, I recieve a compiler error, stating :
error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'const char [60]' (or there is no acceptable conversion)
incomingClass.Text contains the Text string that I want to compare to current class Text character array.
Thank you for your reply.
-Jeff
-
April 28th, 2004, 09:13 PM
#4
Re: Overloaded operators calling each other.
Originally posted by SphereII
Hello peoples.
I'm just learning about C++ classes, and overloading operators. I'm having some trouble with two overloads, one of which is operator==(char * ), and the other one is operator==(const CStr&).
1) Why isn't it operator(const char *)? You aren't changing the value of the pointed to character buffer, so this should be const char *.
2) Do you have any casting operators, for example an overload of operator char *() defined in your class? If you do, then I can see a boatload of compile problems. If this is the case, get rid of these casting operators and try again. The reason these operators are problematic is that you are making the compiler do twists and turns whenever it sees a char * and a CStr in the same contexts.
3) Tell us exactly the errors you're getting (post the actual compile error). Don't just say "it errors out" (and please post the error you received before you tried AssMaster's code).
4) Next time, post your full code so we can see exactly what you are trying to do. With C++, the code that you don't show is more than not, the cause of the problem.
Regards,
Paul McKenzie
-
April 28th, 2004, 09:31 PM
#5
I would also make this suggestion, first, compare the length of the strings, if the length's are different, return false (since does it make sense to compare strings for equality when they're different lengths?). Then, loop through the length of both the strings comparing each character. Something like this:
Code:
bool CStr::operator==(const char* other)
{ int otherSize = strlen(other);
int textSize = strlen(text);
if (textSize != otherSize)
{ return false; }
for (int count = 0; count < textSize; count++)
{ if (text[count] != other[count])
{ return false; }
}
return true;
}
It's not hard to make your own strlen() function if you can't use the one provided by C++.
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html
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
|