|
-
October 24th, 2001, 07:31 PM
#2
Re: overload <= operator
well first of all, if you define the "operator <=" outside a class, you have to precede it with "ClassName::". also there's only one parameter into "operator <=" the other parameter is the Name object itself, or the "this" object. take a look at this small example. it overloads the "<=" operator to base the comparison off the length of the strings, not the characters:
#include <iostream.h>
#include <string.h>
class Name
{
public:
Name(char *c)
{
strcpy (m_name, c);
}
operator <= (Name &a)
{
return strlen(m_name) <= strlen(a.m_name);
}
private:
char m_name[64];
};
void main()
{
Name n("abcdefg");
Name m("h");
if (n <= m)
{
cout << "abcdefg <= h" << endl;
}
else
{
cout << "abcdefg > h" << endl;
}
}
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
|