Last active
August 29, 2015 14:20
-
-
Save mkmkme/de675df33303b378468e to your computer and use it in GitHub Desktop.
operator overloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdint> | |
using namespace std; | |
class IR | |
{ | |
public: | |
uint32_t m_a; | |
IR() : m_a(0) { cout << "IR::IR()" << endl; } | |
IR(int a) : m_a(a) { cout << "IR::IR(int)" << endl; } | |
IR(const IR& mlc1) : m_a(mlc1.m_a) { cout << "IR::IR(const IR&)" << endl; } | |
operator uint32_t() const { cout << "IR::operator uint32_t()" << endl; return m_a; } | |
}; | |
class AW | |
{ | |
uint32_t m_b; | |
public: | |
AW() : m_b(0) { cout << "AW::AW()" << endl; } | |
AW(int b) : m_b(b) { cout << "AW::AW()" << endl; } | |
// explicit operator uint32_t() const { cout << "explicit AW::operator uint32_t()" << endl; return m_b; } // works | |
operator uint32_t() const { cout << "AW::operator uint32_t()" << endl; return m_b; } // doesn't work | |
operator IR() const { cout << "AW::operator IR()" << endl; return IR(m_b); } | |
}; | |
int main(int argc, char const *argv[]) | |
{ | |
cout << "*** AW a;" << endl; | |
AW a; | |
cout << "*** IR b;" << endl; | |
IR b; | |
cout << "*** b = (IR) a;" << endl; | |
b = (IR) a; // doesn't work | |
// cout << "*** b = a.operator IR();" << endl; | |
// b = a.operator IR(); // always works | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment