Liste des Groupes | Revenir à cl c |
On 08/01/2025 17:05, Julio Di Egidio wrote:Sometimes I think the following can be kind of useful:On 08/01/2025 16:16, Ben Bacarisse wrote:<snip>P.S. To be clear, as I am still being a bit quick: I do not also mean "public destructors" should take a const pointer in input, i.e. apply as appropriate...More relevant to a discussion of const is to ask what you plan to do>
with pT since you can't (without a cast) assign any useful value to the
allocated object.
Say my program unit implements AVL trees, with (conceptually speaking) constructors/destructors, navigation and retrieval, and of course manipulation (inserting, deleting, etc.).
>
My idea (but I would think this is pretty "canonical" and, if it isn't, I am missing the mark) is: my public functions take/give "sealed" instances (with const members to const data), as the user is not supposed to directly manipulate/edit the data, OTOH of course my implementation is all about in-place editing...
And here is what my construction/destruction code is looking like at the moment, which should also make clear what I meant by "a private method implementing a public interface" and why:
```c
static AvlTree_t const *AvlTree_node(
void const *pk, AvlTree_t const *pL, AvlTree_t const *pR
) {
AvlTree_t *pT;
pT = malloc(sizeof(AvlTree_t));
if (!pT) {
return NULL;
}
pT->pk = pk;
pT->pL = pL;
pT->pR = pR;
return pT;
}
static int AvlTree_free_(AvlTree_t const *pT) {
assert(pT);
free((AvlTree_t *)pT);
return 0;
}
AvlTree_t const *AvlTree_create(void const *pk) {
return AvlTree_node(pk, NULL, NULL);
}
void AvlTree_destroy(AvlTree_t *pT) {
AvlTree_visitPost(AvlTree_free_, pT);
}
```
-Julio
Les messages affichés proviennent d'usenet.