Sujet : Re: So You Think You Can Const?
De : julio (at) *nospam* diegidio.name (Julio Di Egidio)
Groupes : comp.lang.cDate : 08. Jan 2025, 17:24:05
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vlm8r6$2dkpd$5@dont-email.me>
References : 1 2 3 4 5
User-Agent : Mozilla Thunderbird
On 08/01/2025 17:05, Julio Di Egidio wrote:
On 08/01/2025 16:16, Ben Bacarisse wrote:
<snip>
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...
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...
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