Created
August 17, 2012 11:34
-
-
Save marchelbling/3378197 to your computer and use it in GitHub Desktop.
Object coercion flow in C++
This file contains 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> | |
class B; | |
class A | |
{ | |
public: | |
A () | |
{ std::cout << "A::A ()" << std::endl; } | |
operator B (); | |
B f () const; | |
}; | |
class B | |
{ | |
public: | |
B () | |
{ std::cout << "B::B ()" << std::endl; } | |
B& operator= (B const b) | |
{ | |
std::cout << "B& operator= (B const b)" << std::endl; | |
return *this; | |
} | |
B (A const& a) | |
{ | |
std::cout << "B::B(A const&)" << std::endl; | |
} | |
}; | |
A::operator B () | |
{ | |
std::cout << "A::operator B ()" << std::endl << "\t"; | |
return B (); | |
} | |
B A::f () const | |
{ return A (); } | |
int main () | |
{ | |
B b; // B::B () | |
A a; // A::A () | |
b = a.f(); // A::A () | |
// A::operator B() | |
// B::B () | |
// B& operator= (B const b) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment