You have two distinct variable declarations. The second one goes out of scope immediately, leaving the original declaration as the only one still in effect. You're calling print_type
on the first variable, not the second.
You cannot change the type of a variable. A variable's type is constant. The first t
variable has type BST<countint>
, and the second t
variable has type RST<countint>
. The second variable temporarily hides or shadows the first variable, but that effect goes away after the conditional statement ends (which is on the very next line). Some compilers would have warned you that the second t
wasn't used. (If your compiler warned you, then you should have mentioned that in the question; never ignore a compiler warning.)
If you want t
to refer to values of different types, then you should use pointers. Declare t
as a BST<countint>*
, and then determine how to assign it a value. For example:
BST<countint>* t;
if (rst)
t = new RST<countint>;
else
t = new BST<countint>;