C++ 通过类传递参数?

标签 c++ class param

我正在写一个游戏;与其把我的代码弄得一团糟,我真的很想做这样的事情。

这就是我的代码现在的样子。

bool Verified[18] = { false }; // 18 for (18 clients in game)

我显然不会设置那个 bool

for(int Client;Client<18;Client++)
{
 Verified[Client] = false;
}

我真正想做的是下面这个。

static class Clients
{
//Verified size is 18, for (18 clients max in game)
 bool Verified = the value sent by example below to client?

 //some functions i'd like to add later
}

我想做的是下面这个:

Clients[ClientIndex].Verified = false;
Clients[ClientIndex].SomeFunction_Call( < This param same as ClientIndex);

我对c++的了解不多;我失败了。但任何帮助都会很棒。

最佳答案

首先,C++ 中没有static 类这样的东西。删除它。

现在在您定义类之后(不要忘记;在类的末尾)

class Client {
public:
   bool var;
   void func (int i);
};

你需要创建一个数组(或 vector 或任何东西)

Client clients[10];

然后,你可以像这样使用它:

    for (int i=0; i<10; i++) {
       clients[i].var = false;
    }

或者:

    for (int i=0; i<10; i++) {
        clients[i].func (i);
    }

关于C++ 通过类传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17479586/

相关文章:

c++ - 收到 "enum is not a class or namespace"错误 - C++

node.js - 如何导出具有多个静态方法的类

java - 像这样创建 Java Frame 是一个好习惯吗?

c# - 将 C# 参数传递给 Powershell 脚本 - 无法验证参数 'Identity' 上的参数

youtube - 通过YouTube加载SWF文件

c++ - 从 vector < vector <字符串>>中删除重复值,不区分大小写?由 小码哥发布于

c++ - 为什么 6-7 个线程比 20 个线程快?

java - 一般套接字问题 - 将 C++ 结构从 Java 转移到 C++

c++ - 如何在 C++ 中使用循环依赖类?

javascript - jQuery 将参数附加到 url