M
Michael Lehn
Hi,
I have a question regarding the conversion of objects. When is the
conversion done by the constructor and when by the operator. My feeling
tells me that the constructor is preferred. But I couldn't find the
exact rule in the C++ standard.
And what if the classes have template parameters?
It would be great if somebody could get me a rough hint where in the
standard I should start reading.
I know that it is dangerous to have both a conversion constructor and a
conversion operator. But I want to learn more about this whole
mechanism.
Thanks,
Michael
//---------------------------------------
struct A;
struct B
{
B(const A &a);
};
struct A
{
operator B() const {}
};
B::B(const A &a) {}
int
main()
{
A a;
B b = a; // Uses always the constructor of B if possible?
return 0;
}
//----------------------------------------
I have a question regarding the conversion of objects. When is the
conversion done by the constructor and when by the operator. My feeling
tells me that the constructor is preferred. But I couldn't find the
exact rule in the C++ standard.
And what if the classes have template parameters?
It would be great if somebody could get me a rough hint where in the
standard I should start reading.
I know that it is dangerous to have both a conversion constructor and a
conversion operator. But I want to learn more about this whole
mechanism.
Thanks,
Michael
//---------------------------------------
struct A;
struct B
{
B(const A &a);
};
struct A
{
operator B() const {}
};
B::B(const A &a) {}
int
main()
{
A a;
B b = a; // Uses always the constructor of B if possible?
return 0;
}
//----------------------------------------