2
我想要在兩個層次上進行隱式轉換。以下代碼片段是我面臨的問題的原型。C++中的嵌套隱式轉換
//Sources
class A
{
public:
void print()
{
std::cout <<"Class A"<< std::endl;
}
operator int()
{
return 1;
}
};
class B
{
public:
void print()
{
std::cout <<"Class B"<< std::endl;
}
operator A()
{
return A();
}
};
class C
{
public:
void print()
{
std::cout <<"Class C"<< std::endl;
}
operator B()
{
return B();
}
};
void print_(A a)
{
a.print();
}
//driver
int main(int argc, char* argv[])
{
C c;
//print_(c); // compilation error
//print_(C()); // compilation error
print_(c.operator framework::configuration::B()); //when explicitly invoked it worked
return 0;
}
我看着下面的鏈接提供確信這是可以實現的例子。
How do conversion operators work in C++?
Got it!謝謝。 – jazaman