C++枚举的使用 C99与C11的枚举

本文介绍如何在C++中使用结构体和枚举来定义按钮,并实现按钮的点击事件处理和状态切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


#include <iostream>
using namespace std;

//结构体  按钮
struct UIButton{
    int tag;
    
    void touch(){
        cout << "tag值为 " << tag << " 的Button被点击了" << endl;
    }
};


//枚举  按钮的状态
enum ButtonType{
    UP = 100,
    DOWN,
    LEFT,
    RIGHT
};

//枚举  C++11  这样枚举里面的元素就可以和别的枚举一样名字了
enum class ButtonType2{
    UP = 100,
    DOWn,
    LEFT,
    RIGHT
};

//
void touchButton(UIButton sender){
    sender.touch();
    switch (sender.tag) {
        case UP:
            //
            cout << "向上移动" << endl;
            break;
        case DOWN:
            cout << "向下移动" << endl;
            break;
        case LEFT:
            cout << "向左移动" << endl;
            break;
        case RIGHT:
            cout << "向右移动" << endl;
            break;
            
        default:
            break;
    }
}

//主函数
int main(){
    
    //创建结构体对象
    UIButton button1;
    button1.tag = UP;
    
    UIButton button2;
    button2.tag = DOWN;
    
    UIButton button3;
    button3.tag = LEFT;
    
    UIButton button4;
    button4.tag = RIGHT;
    
    touchButton(button1);
    
   
   // ButtonType2::UP;
    
    
    int x = 100;
    int y = 200;
    
    if (x && y) {
        cout << x << ' ' << y << endl;
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值