![]() |
| | #1 |
| C++0x User Join Date: Nov 2008 Location: Sweden
Posts: 133
| operator== Thanks |
| Tux0r is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #3 |
| C++0x User Join Date: Nov 2008 Location: Sweden
Posts: 133
| It's a bad habit from writing too much C code. |
| Tux0r is offline | |
| | #4 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> Hello, is there a way I can implement the ability to go (a==b==c) on instances instead of (a==b && b==c) As Laserlight pointed out, this would probably be the wrong approach. The best policy is to model conventional semantics in order to avoid confusion and prevent the introduction of unexpected bugs. Having said that, it is of course possible. Code: struct foo
{
foo( int value = 0 )
: value( value )
{ }
struct proxy
{
friend proxy operator == ( proxy const& lhs, foo const& rhs )
{
return lhs.ptr && equal( *lhs.ptr, rhs ) ? lhs : 0;
}
operator bool ( void )
{
return ptr != 0;
}
proxy( foo const* ptr )
: ptr( ptr )
{ }
foo const*
ptr;
};
friend proxy operator == ( foo const& lhs, foo const& rhs )
{
return proxy( equal( lhs, rhs ) ? &rhs : 0 );
}
friend bool equal( foo const& lhs, foo const& rhs )
{
return lhs.value == rhs.value;
}
int
value;
};
int main( void )
{
cout << bool( foo( 1024 ) == foo( 1024 ) == foo( 1024 ) ) << endl;
cout << bool( foo( 0 ) == foo( 1024 ) == foo( 1024 ) ) << endl;
cout << bool( foo( 1024 ) == foo( 0 ) == foo( 1024 ) ) << endl;
cout << bool( foo( 1024 ) == foo( 1024 ) == foo( 0 ) ) << endl;
return 0;
}
|
| Sebastiani is offline | |
| | #5 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #6 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #7 |
| C++0x User Join Date: Nov 2008 Location: Sweden
Posts: 133
| Obfuscation at its best. |
| Tux0r is offline | |
| | #8 |
| C++0x User Join Date: Nov 2008 Location: Sweden
Posts: 133
| By the way is there a STL algo for checking if all elemens in an array are equal? I didn't find one. |
| Tux0r is offline | |
| | #9 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #10 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Or alternatively find_first_of, using not_equal_to bound to the first element. Edit: Hang on, not find_first_of, what am I thinking. What about count? Last edited by tabstop; 07-08-2009 at 10:41 AM. |
| tabstop is offline | |
| | #11 |
| C++0x User Join Date: Nov 2008 Location: Sweden
Posts: 133
| Yeah I tried count but it looks like std::adjacent_find is the best solution. |
| Tux0r is offline | |
| | #12 |
| Hat seller extraordinaire Join Date: Apr 2008
Posts: 159
| What about equal_range? Code: #include <vector>
#include <algorithm>
#include <iostream>
template<class Iterator>
bool AreAllEqual(const Iterator& begin, const Iterator& end)
{
std::pair<Iterator, Iterator> equalRet = std::equal_range(begin, end, *begin);
return ((equalRet.first == begin) && (equalRet.second == end));
}
template<class Container>
bool AreAllEqual(const Container& cont)
{
return AreAllEqual(cont.begin(), cont.end());
}
int main(int argc, char ** argv)
{
std::vector<int> d;
d.push_back(9);
d.push_back(9);
d.push_back(9);
d.push_back(9);
d.push_back(9);
std::cout << "Container: " << std::boolalpha << AreAllEqual(d) << '\n';
int* data = &d[0];
std::cout << "Array: " << AreAllEqual(data, data + d.size()) << '\n';
d.push_back(10);
std::cout << "Container with 10: " << AreAllEqual(d) << std::endl;
data = &d[0];
std::cout << "Array with 10: " << AreAllEqual(data, data + d.size()) << '\n';
}
|
| adeyblue is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #14 | |
| The larch Join Date: May 2006
Posts: 3,082
| Probably the cleanest way would be to define your own function: Code: template <class Iter, class Pred> bool all(Iter from, Iter to, Pred pred);
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #15 |
| C++0x User Join Date: Nov 2008 Location: Sweden
Posts: 133
| Obviously, but I'm not the one who likes to reinvent the wheel. |
| Tux0r is offline | |
![]() |
| Tags |
| class, operator== |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| stl error | eklavya8 | C++ Programming | 8 | 06-30-2008 12:01 PM |
| "error: incomplete type is not allowed" | Fahrenheit | C++ Programming | 9 | 05-10-2005 09:52 PM |
| Problem with Template Function and overloaded equality operator | silk.odyssey | C++ Programming | 7 | 06-08-2004 04:30 AM |
| operator== | Unregistered | C++ Programming | 4 | 03-26-2002 05:24 PM |