In comp.sys.raspberry-pi, Charlie Gibbs wrote:
> On 2021-01-05, Martin Gregorie wrote:
>> Its very useful indeed in Java: its often helpful to use the same name
>> with different parameter lists for constructors and also for methods that
>> all do similar jobs, e.g for outputting values from a class its helpful
>> to use the same method name, with different parameter lists say:
>>
>> getValue(String caption, int value);
>> getValue(String caption, double value);
>> getValue(String caption, boolean value);
Java gets that from C++ doesn't it?
> I find it easier to just cast "value" to a consistent type.
> But then, I'm a hidebound C weenie...
Use a union type with an enum for which value in the value is
the proper one. That can easily expand to new types that are not
easily cast.
enum caption_types {
CAPTION_UNDEF = 0,
CAPTION_INT,
CAPTION_FLOAT,
CAPTION_RETURN
};
struct int_value_t;
struct float_value_t;
struct return_value_t;
typedef struct {
enum caption_types here;
int value;
} int_value_t;
typedef struct {
enum caption_types here;
float value;
} float_value_t;
/* try casting this one to something meaningful... */
typedef struct {
enum caption_types here;
int (*value)();
} return_value_t;
typedef struct {
enum caption_types here;
} id_value_t;
typedef union {
id_value_t id_value;
int_value_t int_value;
float_value_t float_value;
return_value_t return_value;
} compare_value_t;
typedef char String; /* unused here */
int
getCaption(String caption, compare_value_t value)
{
int rv = -1;
if (CAPTION_INT == value.id_value.here) {
rv = value.int_value.value;
}
if (CAPTION_FLOAT == value.id_value.here) {
rv = value.float_value.value;
}
if (CAPTION_RETURN == value.id_value.here) {
/* use the return value of the function provided */
rv = (value.return_value.value)();
}
return rv;
}
Elijah
------
should have used a swtich statement
--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)
|