Domanda

Given a C function:

void f(T x, T y) {
    x = y;
}

I want to make sure that all instances of T assignments will fail. So far, the best solution I have is something like:

#define T const void *

is there a better solution? (Ideally I would like T to be defined as some kind of a non-assignable opaque record pointer type).

È stato utile?

Soluzione

typedef is more suitable here.

For immutable data: typedef const void * T;

For immutable pointer: typedef void * const T;

Altri suggerimenti

I made a dumb mistake. Apparently,

typedef const int T 

or

typedef void * const T

work fine for preventing the assignment.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top