0

我有一个指针数组

myclass* myclass_instances[100];

myclass_instances[i] = new myclass(...);

现在我有另一个class udp_networking. 在这个类的方法中,我想在这些myclass_instances对象上调用一些方法。

我应该如何在其中声明一个成员,我 class udp_networking应该如何初始化它,它指向相同的实例?

4

2 回答 2

1

这应该这样做:

class udp_networking {

    myclass* (*ptr_to_array)[100]; // declare a pointer to an array of 100 myclass*

    explicit udp_networking( myclass* (*ptr)[100] )
        : ptr_to_array(ptr) { }
         // initialize it in constructor
};

用法:

my_class* instances[100] = { /* ... */ };
upd_networking u( instances );

但这是一种非常 C'ish 的处理方式。我会考虑std::vectorstd::array为此。

于 2013-06-30T14:49:35.240 回答
-2
myclass* pointer; // this would be a pointer or an array. The difference
                  // is how you use it. Make shure you keep
                  // that difference in mind while programming

myclass** array_of_pointers; // this would be an array of pointers to myclass
                             // might also be an array of arrays.
                             // or an pointer to an array

myclass*** pointer_to_array_of_pointers; // this would be a pointer to an array of pointers
                                         // or an array of arrays of arrays.
                                         // or an array of arrays of pointers.
                                         // or an array of pointers of arrays
                                         // ...
于 2013-06-30T14:56:30.287 回答