c++ same but different
Mar 1, 2015 at 12:00pm UTC
is there any way to make the compiler thinks that '*' and '*' is different?
Mar 1, 2015 at 12:29pm UTC
I don't understand. Can you give an example?
Mar 1, 2015 at 1:50pm UTC
Hi,
Do you mean overloading the *
operator to do different things depending on what type it is referring to?
Mar 2, 2015 at 4:51am UTC
I don't understand. Can you give an example?
1 2 3
char array[2] = {'*' ,'*' };
if ( array[0] != array[1] ) // i want this to be true if possible
cout << " not equal" ;
Last edited on Mar 2, 2015 at 4:51am UTC
Mar 2, 2015 at 5:15am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
using namespace std;
int main()
{
pair<char ,int > p[2];
p[0]={'*' ,1};
p[1]={'*' ,2};
cout << p[0].first << " " << p[0].second << "\n" ;
cout << p[1].first << " " << p[1].second << "\n" ;
if (p[0] != p[1]) cout << "not equal\n" ;
}
Mar 2, 2015 at 6:08am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <memory>
struct Char
{
constexpr Char( char c = ' ' ) noexcept : value(c) {}
constexpr operator char () const noexcept { return value ; }
operator char & () noexcept { return value ; }
constexpr char as_char() const noexcept { return value ; }
char value ;
};
inline bool operator == ( const Char& a, const Char& b ) noexcept { return std::addressof(a) == std::addressof(b) ; }
inline bool operator != ( const Char& a, const Char& b ) noexcept { return !(a==b) ; }
inline bool operator == ( const Char& a, const char & b ) noexcept { return std::addressof(a.value) == std::addressof(b) ; }
inline bool operator != ( const Char& a, const char & b ) noexcept { return !(a==b) ; }
inline bool operator == ( const char & a, const Char& b ) noexcept { return b == a ; }
inline bool operator != ( const char & a, const Char& b ) noexcept { return !(a==b) ; }
int main()
{
Char array[2] = { '*' , '*' } ;
if ( array[0] != array[1] ) std::cout << "not equal\n" ; // not equal
if ( char (array[0]) == array[1].as_char() ) std::cout << "equal\n" ; // equal
char c = '*' ;
if ( array[0] != c ) std::cout << "not equal\n" ; // not equal
if ( array[0].as_char() == c ) std::cout << "equal\n" ; // equal
array[0] = 'a' ;
if ( array[0].as_char() != char (array[1]) ) std::cout << "not equal\n" ; // not equal
if ( char (array[0]) == 'a' ) std::cout << "equal\n" ; // equal
}
http://coliru.stacked-crooked.com/a/c456f904fc49cb12
Mar 2, 2015 at 10:30am UTC
Lorence30 wrote:1 2 3
char array[2] = {'*' ,'*' };
if ( array[0] != array[1] ) // i want this to be true if possible
cout << " not equal" ;
That is easy:
1 2 3 4 5 6 7 8
bool mydiff( char , char ) {
return true ;
}
// use
char array[2] = {'*' ,'*' };
if ( mydiff( array[0], array[1] ) )
cout << " not equal" ;
However, I have a feeling that there is more to the story.
Is it perhaps that you have larger alphabet, where identicals should be equal, and the * is a special "any" that equals every character except another any?
Topic archived. No new replies allowed.