Todos os livros

Profissional

Apps Sobre Coach Entrar Começar a ler

Computer Science · Glossário

O que é Trait object, fat pointer?

Também chamado de: 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.
Ler no capítulo →