3 ms·
You could overload operator=() in C++ with a call to exit(), which fulfills "variable assignment that halts the program".
by lock1 1y ago
You could overload operator=() in C++ with a call to exit(), which fulfills "variable assignment that halts the program".
- dvt 1y agoI was ignoring these kinds of fancy overload cases, but even in JS you can mess with setters to get some unexpected behavior (code below).
- BobbyJo 1y agoidk if I'd consider overloading the assignment operator to call a function, then using it, actually an assignment in truth.
- eru 1y agoWell, when you read the source of the caller, it looks exactly like a normal assignment.
- pjmlp 1y agoAnd for a Rust contrived example, making += terminate execution, use std::ops::AddAssign; use std::process; #[derive(Debug, Copy, Clone, PartialEq)] struct Point { x: i32, y: i32, } impl AddAssign for Point { fn add_assign(&mut self, other: Self) { *self = Self { x: self.x + other.x, y: self.y + other.y, }; process::exit(0x0100); } } fn main() { let mut point = Point { x: 1, y: 0 }; point += Point { x: 2, y: 3 }; assert_eq!(point, Point { x: 3, y: 3 }); }