Todos los libros

Profesional

Apps Acerca de Coach Iniciar sesión Empezar a leer

Computer Science · Glosario

¿Qué es Trait object, fat pointer?

También llamado: trait object · fat pointer

Definition 8.6 University Computer Science — Year 2 · Capítulo 8 — Traits and Generics

A trait object dyn Trait is a value of some type implementing Trait, whose type is known only at run time. It is used behind a pointer, &dyn Trait or Box<dyn Trait>, which is a fat pointer: the address of the value together with the address of a table, the vtable, holding the value type’s size, alignment, drop function and the addresses of its implementations of the trait’s methods.

Where the table pointer lives. In Rust it travels in the reference, so a Circle stays 8 bytes and gains no hidden field; only the references used as trait objects grow, to 16 bytes. In C++ every object of a class with virtual functions carries it, whether or not it is ever called virtually.
Where the table pointer lives. In Rust it travels in the reference, so a Circle stays 8 bytes and gains no hidden field; only the references used as trait objects grow, to 16 bytes. In C++ every object of a class with virtual functions carries it, whether or not it is ever called virtually.
Leer en el capítulo →