take adress of operator?

hi

can i take the address of a operator function ???
e.g:

bool operator==(const X&a, const X&b ){ ... }

which is a non-member function in a namespace N.
Yes, you can.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

struct MyStruct {}; struct MyOtherStruct {};

namespace N
{

const char * operator == (const MyStruct      &, const MyStruct      &) { return "true";  }
const char * operator == (const MyOtherStruct &, const MyOtherStruct &) { return "false"; }

}

int main()
{
    const char * (*pf1)(const MyStruct      &, const MyStruct      &) = N::operator ==;
    const char * (*pf2)(const MyOtherStruct &, const MyOtherStruct &) = N::operator ==;

    std::cout << pf1(MyStruct(),      MyStruct()     ) << std::endl;
    std::cout << pf2(MyOtherStruct(), MyOtherStruct()) << std::endl;
}
Last edited on
8
9
const char * operator == (const MyStruct      &, const MyStruct      &) { return "true";  }
const char * operator == (const MyOtherStruct &, const MyOtherStruct &) { return "false"; }
LOL
I know... I should probably have used boolalpha...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

struct MyStruct {}; struct MyOtherStruct {};

namespace N
{

bool operator == (const MyStruct      &, const MyStruct      &) { return true;  }
bool operator == (const MyOtherStruct &, const MyOtherStruct &) { return false; }

}

int main()
{
    bool (*pf1)(const MyStruct      &, const MyStruct      &) = N::operator ==;
    bool (*pf2)(const MyOtherStruct &, const MyOtherStruct &) = N::operator ==;

    std::cout << std::boolalpha;

    std::cout << pf1(MyStruct(),      MyStruct()     ) << std::endl;
    std::cout << pf2(MyOtherStruct(), MyOtherStruct()) << std::endl;
}
Last edited on
if the operator is on the global workspace it isn't working right?
It can work in the global namespace ;)
Topic archived. No new replies allowed.