C++ C++;通过具有数组索引的类传递参数/

C++ C++;通过具有数组索引的类传递参数/,c++,class,parameter-passing,C++,Class,Parameter Passing,例如,我可以通过下面这样的类传递参数 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")

例如,我可以通过下面这样的类传递参数

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<iostream>


class cat
{
    int ID;
    static int IDTracker;
public:
    cat();
    void dog(char* value);
};

int cat::IDTracker = 0;
cat::cat()
{
    this->ID = cat::IDTracker;
    cat::IDTracker++;
}
void cat::dog(char *value)
{
    std::cout << value << this->ID;
}


int main()
{
    cat cats[18];
    cats[1].dog("Second instance, with index value: ");
    cats[2].dog("Third instance, with index vlaue: ");

    return 0;
}
#包括
班猫
{
int-ID;
静态积分跟踪器;
公众:
cat();
无效狗(字符*值);
};
int cat::IDTracker=0;
猫::猫()
{
这->ID=cat::IDTracker;
cat::IDTracker++;
}
无效猫::狗(字符*值)
{

我不完全确定这是否有效,但它的一些变体可能

#include <stdio.h>
#include <stdlib.h>

void *foo;

class cat
{
  public:

  void dog(char *value) // int ID I'd like to be the index array it was called from?
  {
    int ID = (this - ((cat *)foo))/(sizeof(cat));
    printf("%d - %s",ID, value);
  }

};


int main ( void )
{
   cat cats[18];
   foo = &(cats);
   cats[1].dog("Fido");
}
#包括
#包括
void*foo;
班猫
{
公众:
void dog(char*value)//int ID我想成为从中调用它的索引数组吗?
{
int ID=(this-((cat*)foo))/(sizeof(cat));
printf(“%d-%s”,ID,值);
}
};
内部主(空)
{
猫科动物[18];
foo=&(猫);
猫[1]。狗(“Fido”);
}

我对此进行了测试,效果良好:

#include <vector>

// Forward declaration of the class
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.
        }
};

// Static variables need to be defined.
int Cat::ID = 0;

class CatArray {
    private:
        std::vector<Cat> cats;

    public:
        // explicit means that the argument passed to this constructor must 
        // be an unsigned int. The ": cats(size)" part is an initializer 
        // list that initializes the cats vector so that it would have the 
        // specified size.
        explicit CatArray(unsigned int size) : cats(size) {}

        Cat& operator [](unsigned int index) {
            Cat::ID = index;
            return cats[index];
        }
};

此方法适用于您希望声明的任意数量的数组。

我不明白您想要的是什么。请重新措辞,这对我来说没有意义。这就是为什么要将类和方法命名为同一个名称?这会使您更难知道要做什么。我想在传递的索引arra的类中存储一个名为INT的IDy、 “通过类传递参数”是什么意思?您可能会对类的概念感到困惑。我所要做的就是从类数组索引中传递dog的第一个参数,在本例中为1。如果它是1,我会马上尝试。我看到您清楚地理解了我的问题。这是我的特例-iKonroi的开发(192.168.1.146)线程0xF9000004“”上发生异常。异常:0xC0000005地址:0x9802378参数:0x00000000 0x883A8810访问冲突读取0x883A8810的内存。我真的不知道这将如何工作,但我知道的是在类中为值定义int是非法的。我想它会工作的…静态成员变量已经支持很长时间了…ID也会或设置它是哪一个cat。应该为cat包含一个成员变量,该变量被分配静态的值。@ojblass到您的第一条注释:
您没有新建cat,构造函数没有被调用
-这是错误的。
cat[18];
分配和构造18个
Cat
对象。@vulcanraven我已将您的问题回滚到它的第一个版本。ojblass对
的错误评论误导了您-您更新的代码甚至没有编译。
CatArray cats(18);

cats[1].dog("oh lol hi");