class apple
{
public :
operator orange () const { cout << "operator"; }
} ;
class orange
{
public :
orange (apple &x ){cout <<"constructor";}
};
void f(orange o)
{
cout <<"function can accept only oranges ";
}
int main()
{
apple a;
f(a);
}
Output:
Compiler Error
Why does this code gives compiler error on g++ compiler? Why isn’t the constructor for orange called when function f is called?
You have a cyclic dependency:
I’m surprised you’re not getting compiler errors. Anyway, you need to define
orangebefore you defineapple, and you don’t need the operatororange:This should work.