c - 在不使用指针的情况下模拟 C 中的多态性

标签 c polymorphism

TL;博士:

是否有一种类型(让我们将其命名为 convert_type),即使在处理 float 类型时,也可以在不丢失信息的情况下进行类型转换?

使用示例:

float f1 = 12.34;
long i1 = 32;

convert_type tmp1 = (convert_type) f;
convert_type tmp2 = (convert_type) i;

float f2 = (float) tmp1; // should be 12.34
long i2 = (long) tmp2; // should be 32
<小时/>

上下文:

我想创建这样一个函数:

convert_type ask(char* question, val_type input_type)

类型val_type定义为:

typedef enum {
    INT_T,
    STR_T,
    CHAR_T,
    FLOAT_T
} val_type;

参数问题有一个要打印的字符串,以便询问输入。

此函数的想法是在 stdin 中询问 input_type 类型的输入,并再次询问,直到输入有效。

ask() 的使用示例:

int main(int argc, char const *argv[]) {
    int nb = (int) ask("How many tries to you want ?\n --> ", T_INT);
    char * message = (char*) ask("What is the victory message ?\n --> ", T_STR);
    float prob = (float) ask("What is the probability of winning ?\n --> ", T_FLOAT);
    printf("\n\n");
    printf("Number of tries : %d\n", nb);
    printf("Victory message : %s\n", message);
    printf("Probability : %f\n", prob);
    return 0;
}

我使用unsigned long类型实现了它,但我在使用float类型时遇到了麻烦。

执行示例:

How many tries to you want ?
 --> many
The input must be a valid integer.
How many tries to you want ?
 --> -2131 
What is the victory message ?
 --> We are the champions !!!
What is the probability of winning ?
 --> 3.4.3
The input must be a valid float value.
What is the probability of winning ?
 --> 2.54


Number of tries : -2131
Victory message : We are the champions !!!
Probability : 2.000000

值得注意的是,unsigned long 不适用于 float 类型(2.54 变成了 2.00因为我猜 unsigned long 包含一个整数值)。

我可以使用什么类型?

最佳答案

如果你足够小心不要导致读/写类型不匹配, 你可以使用union这样做:

#include <stdio.h>

typedef union {
    int int_data;
    char* str_data;
    char char_data;
    float float_data;
} convert_type;

typedef enum {
    INT_T,
    STR_T,
    CHAR_T,
    FLOAT_T
} val_type;

convert_type ask(char* question, val_type input_type) {
    static char[] str = "We are the champions !!!";
    convert_type ret;
    fputs(question, stdout);
    /* this is examples of how to set values. reading is omitted. */
    switch (input_type) {
        case INT_T:
            ret.int_data = -2131;
            break;
        case STR_T:
            ret.str_data = str;
            break;
        case CHAR_T:
            ret.char_data = 'a';
            break;
        case FLOAT_T:
            ret.float_data = 2.54;
            break;
    }
    return ret;
}

int main(int argc, char const *argv[]) {
    int nb = ask("How many tries to you want ?\n --> ", INT_T).int_data;
    char * message = ask("What is the victory message ?\n --> ", STR_T).str_data;
    float prob = ask("What is the probability of winning ?\n --> ", FLOAT_T).float_data;
    printf("\n\n");
    printf("Number of tries : %d\n", nb);
    printf("Victory message : %s\n", message);
    printf("Probability : %f\n", prob);
    return 0;
}

struct不同的是,union的成员共享一个内存空间。 因此,当您向 union 的成员写入一些值时, union的其他成员失效。

关于c - 在不使用指针的情况下模拟 C 中的多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59226369/

相关文章:

C,使用 argv[] 作为命令行参数

c++ - 从 HDD for Linux 快速读取 - 奇怪的现象

c++ - 添加 printf 语句时出现运行时错误

c++ - C/C++ 中用于 AVR 的数组不断追加

c++ - 为什么通过基类指针初始化派生类与通过派生类指针初始化不同?

c++ - C/C++ ASCII 字符到 HEX BYTE 数组

java - java中的方法重载和继承

java - 为什么 "synchronized"对多态没有作用

c++ - 基类指针,基于派生类型调用方法

r - 在 R 中重载 & 运算符