Hello, I am really new for C ++ and came hare from Java (it's not me, it's my university program) And I stopped on one task that is really hard to me to implement on C ++
, I already made simple classes and extending , but with templates something goes wrong. Can u show me how to implement this task correctly?
The base class template
template <class T> class Num {};
includes protected the ad's value type t , and in the open part - virtual method Print () output of values on the screen. Define derived classes - Square_Num and Sqrt_Num. In class Square_Num virtual method Print () displays the square value X, and the class Sqrt_Num - square root of x. The main function of a pointer to the base class Num, which consistently addresses initialized objects derived classes created in the dynamic memory. Based on the mechanism of identification types (class typeinfo) determine the class name, which really points to a pointer. Use the link on base class for virtual methods.
OP: in the previous program I'd actually provided the more general solution i.e the derived classes were also template classes. Below the derived classes are not template classes - notice the key differences (a) the base class is specialized in the declaration of the derived classes, not in main() as previous and (b) in main() - the new operator on the derived classes don't require any typename qualifications:
here's an extension to the above using (a) template abstract base class (ABC), (b) template child classes inherited from the template ABC, (c) template grand-child classes inherited from the template child classes and (d) run-time type identification (RTII) using std::conditional:
To my understanding, there's no RTTI in the last case. std::conditional has nothing to do with that -- the meaning of the type std::conditional<A,B,C>::type depends on the value of the non-type template parameter A, which is (always) a constant expression.
Max: on further thought I think there's something to your observation. The program does not RTII the variable num but rather the subsequent variable created by the new operator. Though the two are related, this point should also be taken into a/c. Thanks
The if-else on std::conditional linked to the static data members of PosNum, NegNum achieves the RTII in this case
In C++, "type" is the property of expressions. Expressions have either dynamic or static type. At compile time (when templates are instantiated) only static type is considered.
More generally, template metacode does all it's work at compile time -- "run-time type" can't have anything to do with it.
With enough work you could unfold the metaprogram yourself. This is evidence that dynamic type isn't involved. In this case it's easy -- just replace every occurrence of typename std::conditional<true, PosNumSq<int>, NegNumSq<int>>::type with PosNumSq<int>.
The program does not RTII the variable num but rather the subsequent variable created by the new operator.
I think you're confusing dynamic dispatch (calling the right virtual function implementation) with RTTI.