【问题标题】:C++ Pass a argument through a class with array index/C ++通过具有数组索引/的类传递参数
【发布时间】:2013-07-06 06:00:13
【问题描述】:

无论如何我可以通过如下类传递参数。

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");

【问题讨论】:

  • 我不明白你想要什么。换个说法,这对我来说没有意义。原样
  • 你为什么把类和方法命名为同一个东西?这让你更难说出你想要做什么。
  • 我想在作为传递的索引数组的类中存储一个名为 ID 的 INT。
  • “通过类传递参数”是什么意思?您可能对类的概念感到困惑。
  • 我要做的就是从类数组索引中传递 dog 的第一个参数,在这个例子中是 1。

标签: c++ class parameter-passing


【解决方案1】:

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

#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];
        }
};

现在像这样使用它:

CatArray cats(18);

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

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

【讨论】:

    【解决方案2】:

    您可以使用静态变量并在构造函数中递增它,以跟踪所有实例:

    #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。
    • @ojblass 对于您的第一条评论:you didnt new the cats the constructor is not called - 这是错误的。 cat cats[18]; 分配并构造了 18 个 Cat 对象。
    • @vulcanraven 我已将您的问题回滚到第一个版本。您被 ojblass 对 new 的错误评论误导了 - 您更新的代码甚至没有编译。
    【解决方案3】:

    我不完全确定这会起作用,但它的一些变体可能......

    #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");
    }
    

    【讨论】:

    • 我过会儿试试,如果真的不好,我会很高兴。我看你很清楚我的问题。
    • That trow's me 异常 - iKonroi 的 DEV (192.168.1.146) 线程 0xF9000004 "" 上发生异常。异常:0xC0000005 地址:0x98022378 参数:0x00000000 0x883A8810 访问冲突读取内存在 0x883A8810。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2020-07-30
    • 2012-04-17
    • 2019-06-29
    相关资源
    最近更新 更多