why aren't implicit type conversions transitive?
after a nice discussion with jdong on irc, we're wondering why..
Code:
struct Number {
Number(const int &n) {
num = n;
}
int num;
};
Number foo = 42;
struct SpecialNumber {
SpecialNumber(const Number &n) {
num = n.num;
}
int num;
};
// this works fine
SpecialNumber bla1 = Number(42);
// won't compile
SpecialNumber bla2 = 42;
..the second assignment at the end does not automatically cast to Number(42)?
Quote:
<jdong> bazkie: but the TRUE question is, what does C++ mean from an OOP standpoint when it lets you say "Number n = 42" ?
as in, a
Number bla(42);
would seem logical: it's a number, initialized by some value.
however, a
Number bla = 42;
seems ugly: bla IS NOT an integer itself, so why does C++ allow this?
concluding, it's a 2-sided question:
1. if C++ allows the Number bla = 42 way of calling a constructor, shouldn't it allow for a SpecialNumber bla = 42 as well?
2. should not C++ disallow for both methods of calling a constructor for a more aesthetically sound OOP design?