c++ - 将函数的默认参数设置为数组

标签 c++ arrays function

<分区>

Possible Duplicate:
Default values for array arguments

如何将数组作为函数的默认参数?我试过这个:

void drawCircle(float radius, GLfloat colour[3]={2.0, 3.0, 4.0}, bool do=true) {
...
}

GLfloat colour[3]={2.0, 3.0, 4.0} 部分给我一个错误。这在 C++ 中可能吗?

最佳答案

在 C++ 中,您不能按值将完整的内存块作为参数传递给函数,但您可以传递其地址。

所以不,至少在 C++03 中你不能这样做。

你可以这样做:

GLfloat defaultColor[3] = {2.0, 3.0, 4.0}; 

void drawCircle(float radius, GLfloat colour[3]= defaultColor, bool do=true)
{
}

关于c++ - 将函数的默认参数设置为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7239834/

相关文章:

c++ - 结构成员的默认值

c++ - 不明白 array-1 是什么意思

arrays - 在 MIPS 中对整数进行排序

python - KDB:如何在 Python 中使用 KDB 函数?

c++ - 以元音、辅音开头的单词的读数

c++ - 来自其他链接单元的垃圾

c++ - KDE Qt 许可问题信息是否具有误导性或 Qt.io 是错误的?

c - 二维数组中对角线的总和

C - 在函数中分配矩阵

oracle - PL/SQL : Retrieve names of procedures and functions within a package