gpt4 book ai didi

c - Vala - 常量而不是魔数(Magic Number)

转载 作者:太空宇宙 更新时间:2023-11-04 03:34:59 24 4
gpt4 key购买 nike

在 C 中我可以做类似的事情

#define SIZE 16
int c[SIZE];

但当我在瓦拉时

const int SIZE = 16;
int c[SIZE];

我在编译期间遇到以“未在此处声明(不在函数中)”结尾的错误

有什么办法可以去掉vala中的魔数(Magic Number),用常量代替吗?

最佳答案

动态分配是要走的路:

const int SIZE = 16;
int[] c = new int[SIZE];

特别是如果 SIZE 是您通过 vapi 文件绑定(bind)到的某些 C 头文件的一部分。

在 vapi 的情况下,静态分配同样有效:

mylib.h

#define MYLIB_SIZE 16

我的库.vapi

namespace Mylib {

// You can optionally specify the cname here:
//[CCode (cname = "MYLIB_SIZE")]
const int SIZE;
}

main.vala

int c[Mylib.SIZE];

关于c - Vala - 常量而不是魔数(Magic Number),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33361244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com