use of struct vs class

drug drug2004 at bk.ru
Sat Mar 7 14:17:54 UTC 2020


07.03.2020 15:58, Steven Schveighoffer пишет:
> 
> Hm... I'd say:
> 
> 1. Don't use a pointer for the element. Just use the struct directly. 
> Using a pointer is bad because it's now going to compare pointers, and 
> not the element data. Not only that, but RBNodes are stored as 
> heap-allocated structs, so you are wasting a lot of memory by allocating 
> another heap allocated thing to get stored inside there.
> 
You can use predicate to solve the issue like you suggest below. But I'm 
totally missed that RBNodes are heap-allocated, thanks!

> 2. RedBlackTree allows you to identify the relationship that you 
> consider unique by providing a "less" function. Instead of instrumenting 
> your Deb type, which might affect other usages, just do:
> 
> RedBlackTree!(Deb, (a, b) => a.name < b.name)
>  > No need to add opCmp and opEquals (if that doesn't make sense in other
> contexts).
> 
> -Steve




More information about the Digitalmars-d-learn mailing list