Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about the C++ dynamic cast with template class

I have base class A, class B inherits from A, class C which is a template class inherits from A. class D inherits from C. The relation is as follows:

        A
       / \   
      B   C (template class)
           \
            D

I can create an A object O1 using D object D1, then I want to dynamic cast O1 to a type C object C1. But I find it fails. My question is why this process fails?

Then I use static_cast to create C type object C2 from D object D1, and I checked C2 has right value from D2. Is it always successful to convert from D type to C type using static_cast?

Thank you!!!

like image 518
William Avatar asked Aug 28 '12 16:08

William


People also ask

What is dynamic cast in C?

Dynamic Cast: A cast is an operator that converts data from one type to another type. In C++, dynamic casting is mainly used for safe downcasting at run time. To work on dynamic_cast there must be one virtual function in the base class.

What is the use of dynamic cast operator?

The primary purpose for the dynamic_cast operator is to perform type-safe downcasts. A downcast is the conversion of a pointer or reference to a class A to a pointer or reference to a class B , where class A is a base class of B .

What is a class template in C?

Templates Specialization is defined as a mechanism that allows any programmer to use types as parameters for a class or a function. A function/class defined using the template is called a generic function/class, and the ability to use and create generic functions/classes is one of the critical features of C++.

What is the use of template class?

Class Templates like function templates, class templates are useful when a class defines something that is independent of the data type. Can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc. Following is a simple example of a template Array class.


2 Answers

Well, since C is a template, it isn’t a type and cannot be used as a target of a cast (neither dynamic to static), and you cannot derive from it. You need to instantiate the class template. The resulting class can be used in a cast. That is, the following will work:

struct A { };
template <typename T> struct C : A { };
struct D : C<int> { };

D d;
A& a = d;
C<int>& c = static_cast<C<int>&>(a); // or dynamic_cast, if `A` were polymorphic
like image 165
Konrad Rudolph Avatar answered Oct 29 '22 23:10

Konrad Rudolph


to a type C object C1. But I find it fails.

Since C is a template, there really is no "type C". Rather there is C<int> or C<Foo> (assuming you have one template argument). Templates only become classes when they are specialized.

So if D inherited from a specific type of C:

 class D : public C<int>
 {
 };

You could dynamic_cast up to a C<int> but not to say a C<float>.

To help better explain, your inheritance tree is really

         A
       / | \
C<float>... C<int> 
             | 
             D

So C isn't a parent of D, but C<int> is (in this example). Instances of C don't really exist in runtime, it's only instances fully specified of C<type> that actually exist.

like image 9
Doug T. Avatar answered Oct 29 '22 23:10

Doug T.