Originally I like to use something like this:
(true?a:b).test()
instead of
(true?a.test():b.test())
in order to save typing time if the function has the same name, initially I thought it should be valid, but I found:
#include <stdio.h>
class A{
public:
char test(){
return 'A';
}
};
class B{
public:
char test(){
return 'B';
}
};
int main(){
printf("%c\n",(false?A():B()).test());
return 0;
}
cannot compile, but if B
is subclass of A
:
#include <stdio.h>
class A{
public:
char test(){
return 'A';
}
};
class B : public A{
public:
char test(){
return 'B';
}
};
int main(){
printf("%c\n",(false?A():B()).test());
return 0;
}
it can compile, why?