6 ms·
Classes in C++ have methods too. The problem is that you can't add new methods to an existing class.
by Kranar 1y ago
Classes in C++ have methods too.
The problem is that you can't add new methods to an existing class.
- kbolino 1y agoMore specifically, you cannot add new abstract (aka "pure virtual") methods to an existing class/interface/trait when that class/interface/trait is already extended/implemented by code you don't control.
- bitwize 1y agoRust lets you combine multiple traits per type.
- Kranar 1y agoC++ lets you inherit from multiple classes as well. I don't see how this has anything to do with being able to add new methods to existing types.
- tomjakubowski 1y agoThere is an important difference: in Rust you can write a new trait, with new methods, and impl that trait for a type without having to change the existing structs/enums which comprise that type at all. You can also do this (with some restrictions, "the orphan rule") for types defined in another library. In C++ classes, you have to be able to modify a class definition to extend it with new methods (or a new ancestor to multiply inherit from).
- bitwize 1y agoC++ classes are types. Rust traits are applied to types.
- Kranar 1y agoAnd what exactly do you think traits apply to types exactly? If your answer doesn't start with an "m" and end with a "ethod", then you may need to re-read the Rust book found here: https://doc.rust-lang.org/book/ch10-02-traits.html https://doc.rust-lang.org/book/ch10-02-traits.html
- adastra22 1y agoYou can add traits (with their methods) to existing types, without modification.
- mrkeen 1y agoSo hypothetically, if you could add new methods to an existing class, it would solve the problem?
- Kranar 1y agoNew virtual methods, yes.