+ Class/Inheritance has challenges
+
+ Combining Data + Code
+
class MyBase {
protected:
int useless;
@@ -20,35 +18,46 @@
};
};
- You have to know if this is an "interface", abstract class, or regular class.
- (it's an abstract class, since makeSound
is pure virtual)
-
-
- A class can be one of many things
-
- - Interface - abstract class with no members, and purely virtual
- - Abstract Class - class with some pure virtual methods
- - "Regular" Class - class that is standalone and is fully implemented
-
- These concepts all exist under the class
keyword
-
+
+ You have to know if this is an "interface", abstract class, or regular
+ class.
+
+ (it's an abstract class, since makeSound
is pure
+ virtual)
+
A class can be one of many things
+class
keyword
+
+ Traits are Interfaces
-
+
+ Traits are Interfaces
+
pub trait Mammal {
fn get_temp(&self) -> i32;
}
-
-
- ... that are explicitly implemented
-
+
+
+ ... that are explicitly implemented
+
pub struct Cat {
age: i32,
outdoor: bool,
@@ -63,11 +72,10 @@
}
}
-
-
-
- And can be used statically or dynamically!
-
+
+
+ And can be used statically or dynamically!
+
fn static_mammal<T: Mammal>(m: T) {
// the type of T must be known at compile time.
}
@@ -75,6 +83,5 @@
// This uses a vtable to dispatch at runtime.
}
-
+