class MyBase {
protected:
int useless;
public:
virtual void makeSound() = 0;
virtual int get_thing() {
return -1;
};
};
You have to know if this is an "interface", abstract class, or regular class.
(it's an abstract class, sincemakeSound
is pure
virtual)
A class can be one of many things
class
keyword
Traits are Interfaces
pub trait Mammal {
fn get_temp(&self) -> i32;
}
... that are explicitly implemented
pub struct Cat {
age: i32,
outdoor: bool,
}
impl Mammal for Cat {
fn get_temp(&self) -> i32 {
if self.outdoor {
65
} else {
72
}
}
}
And can be used statically or dynamically!
fn static_mammal<T: Mammal>(m: T) {
// the type of T must be known at compile time.
}
fn dyn_mammal(m: &dyn Mammal) {
// This uses a vtable to dispatch at runtime.
}