Skip to content

Instantly share code, notes, and snippets.

@marchelbling
Created August 17, 2012 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marchelbling/3378197 to your computer and use it in GitHub Desktop.
Save marchelbling/3378197 to your computer and use it in GitHub Desktop.
Object coercion flow in C++
#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