In the C++ programming language, the dynamic_cast operator is a part of the run-time type information (RTTI) system that performs a typecast. However, unlike an ordinary C-style typecast, a type safety check is incurred at runtime, and it will either throw an exception (when dealing with references) or return a null pointer (when dealing with pointers) if the types are not compatible.
| Property | Value |
| dbpprop:abstract
|
- In the C++ programming language, the dynamic_cast operator is a part of the run-time type information (RTTI) system that performs a typecast. However, unlike an ordinary C-style typecast, a type safety check is incurred at runtime, and it will either throw an exception (when dealing with references) or return a null pointer (when dealing with pointers) if the types are not compatible. Thus, dynamic_cast behaves more like a typecast in a programming language such as Java, rather than the C-style casting which performs no runtime check. dynamic_cast is relatively expensive at runtime and is generally a sign of poor designCitation needed|date=August 2009. It should be avoided wherever possible. Consider using virtual functions, a visitor interface or an enum of types.
- Il costrutto dynamic_cast permette di convertire un puntatore ad un oggetto di una classe base ad uno di classe derivata verificando a run-time se l'operazione è valida. Qualora l'operazione non sia valida il costrutto ritorna un puntatore a NULL. Esempio: class Padre ..... ; class Figlio : public Padre ..... ; class Nipote : public Figlio ..... ; main Padre *p, *pObj = new Padre; Figlio *f, *fObj = new Figlio; Nipote *n, *nObj = new Nipote; p = nObj; // ok un puntatore ad una classe derivata può essere assegnato ad uno di classe base n = p; // qui il compilatore genera errore perché non sa se l'assegnamento è valido (lo si sa solo run-time) n = dynamic_cast<Nipote>(p); // qui si chiede di fare il controllo run-time per validare la conversione (valida in questo caso) if (n) cout << "conversione eseguita con successo"; p = fObj; n = dynamic_cast<Nipote>(p); // qui si chiede di fare il controllo run-time per validare la conversione (non valida in questo caso) if (!n) cout << "conversione fallita"; Si tenga presente che affinché il costrutto dynamic_cast lavori correttamente è necessario che il supporto RTTI del compilatore sia attivo (spesso, nella produzione del firmware, si sceglie i non attivare RTTI poiché questa funzionalità assorbe una quantità di risorse non trascurabile).
- В языке программирования C++, оператор dynamic_cast является частью механизма динамической идентификации типа данных, который позволяет выполнять приведение типа данных. В отличие от обычного приведения типа в стиле Си, проверка корректности приведения типов производится во время выполнения программы. Оператор dynamic_cast может быть применён к указателям или ссылкам. В случае если осуществляется преобразование указателя к типу данных, который не является фактическим типом объекта, в результе преобразования будет получен нулевой указатель. При работе со ссылками при невозможности преобразования типа будет сгенерировано исключение. Таким образом, оператор dynamic_cast обнаруживает сходство в процедуре приведения типа с таким языком программирования как Java, в отличие от Си, в котором не выполняется проверка корректности приведения типа во время выполнения программы.
|
| dbpprop:hasPhotoCollection
| |
| dbpprop:reference
| |
| rdfs:comment
|
- In the C++ programming language, the dynamic_cast operator is a part of the run-time type information (RTTI) system that performs a typecast. However, unlike an ordinary C-style typecast, a type safety check is incurred at runtime, and it will either throw an exception (when dealing with references) or return a null pointer (when dealing with pointers) if the types are not compatible.
- Il costrutto dynamic_cast permette di convertire un puntatore ad un oggetto di una classe base ad uno di classe derivata verificando a run-time se l'operazione è valida. Qualora l'operazione non sia valida il costrutto ritorna un puntatore a NULL. Esempio: class Padre ..... ; class Figlio : public Padre ..... ; class Nipote : public Figlio .....
- В языке программирования C++, оператор dynamic_cast является частью механизма динамической идентификации типа данных, который позволяет выполнять приведение типа данных.
|
| rdfs:label
|
- Dynamic cast
- Dynamic cast
- Dynamic cast
|
| owl:sameAs
| |
| skos:subject
| |
| foaf:page
| |
| is dbpprop:redirect
of | |