【发布时间】:2014-08-17 04:46:16
【问题描述】:
class cat
{public:
void dog(int ID, char *value) // int ID I'd like to be the index array it was called from?
{
debug(ID, value);
}
}
cat cats[18];
cats[1].dog("value second arg, first arg auto filled from index array");
我想要类似的东西。
include <vector>
class CatArray;
class Cat {
// This line means that the CatArray class can
// access the private members of this class.
friend class CatArray;
private:
static int ID;
public:
void dog(const char* value) {
// Use ID here any way you want.
}
};
int Cat::ID = 0;
class CatArray {
private:
std::vector<Cat> cats;
public:
explicit CatArray(unsigned int size) : cats(size) {}
Cat& operator [](unsigned int index) {
Cat::ID = index;
return cats[index];
}
};
但有点不同。一个游戏中有 18 个客户端,我基本上需要这样做。例如,“客户端 4 选择一个选项,然后通过数组索引调用该选项,然后客户端 4 将调用具有索引 4 的函数的函数”
【问题讨论】:
-
你的问题是......?
-
我真的无法理解这一点;猫和狗的类比令人困惑(似乎您确实将“猫”和“狗”用作诸如“foo”和“bar”之类的无意义的词)。但我觉得你应该只使用
std::map。即使地图不是正确使用的东西,原始数组和字符指针也肯定是错误的。