jeudi 29 décembre 2016

How to make implicit conversion constructor call over 2 class in C++?

Here is the test code:

class A 
{
public:
    A(const int& i){}
};

class B
{
public:
    B(const A& a) {};
};


int main() 
{
    B b = 1;
    return 0;
}

It comes to error: "No viable conversion from int to B".

It's obviously that int can be converted to B through being converted to A. But it seems that C++ does not search the constructor inherited tree.

Note that B is not inherited from A.

Is there any way to make it possible without adding a int constructor for B manually?

Aucun commentaire:

Enregistrer un commentaire