0

I have a array of pointers

myclass* myclass_instances[100];

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

Now I have another class udp_networking. Inside the methods of this class I want to call some methods on these myclass_instances objects.

How should I declare a member in this class udp_networking and how should I initialize it, which points to the same instances?

  • 1
    I thought you said it was global, so why do you need a member to point to them? I would like to point out that global variables are generally considered bad practice. – Borgleader Jun 30 '13 at 14:49
  • I changed the question...i dont want to use it global anymore –  Jun 30 '13 at 14:51

2 Answers2

1

This should do:

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

Usage:

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

But that's a very C'ish way to go about things. I'd consider std::vector or std::array for this.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • how should I define the constructor parameter? –  Jun 30 '13 at 14:56
  • If you want to go this way, you might consider using a reference to an array instead: `myclass* (&ptr_to_array)[100]`. However, there's still a question of whether you want to fix the array size. If not, then a plain `myclass**` might be a better idea. – AnT stands with Russia Jun 30 '13 at 15:08
-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
                                         // ...
Wolfgang Skyler
  • 1,338
  • 1
  • 13
  • 26
  • That's quite a lot of "or"s. Pointers of pointers are not 2d arrays. – jrok Jun 30 '13 at 15:01
  • You should not equate a pointer to a pointer with a pointer to an array. – Borgleader Jun 30 '13 at 15:02
  • @jrok: http://www.codeproject.com/Articles/21909/Introduction-to-dynamic-two-dimensional-arrays-in http://www.cplusplus.com/articles/NAUq5Di1/ http://www.hawkee.com/snippet/9469/ They all declare 2D arrays with `**`. (when not using `std::vector`) And that happens to be a "pointer of pointers". More results on Google if you happen to feel like searching. ---@Borgleader: Would you mind giving me your reasoning? I'm no more than another fool after all. And that wont change by blindly accepting what I'm told. – Wolfgang Skyler Jun 30 '13 at 20:21
  • And they all spread misinformation. I recommend this [SO thread](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810676#4810676) instead. – jrok Jun 30 '13 at 21:29
  • While how he does it is still valid, it has a problem of not being variable length. As he states in his code comments, "ISO C++ forbids variable length array". So since `int n[x];` is invalid. we use `int* y = new int[x];` And that gives us an array of `int`s. So if `int*` is an array of int's, and we need an array of an array of `int`s, we get `int** x = new int*[x];` for the first array. and then initalize each array (`int*`, as defined) in that array. And I'm sorry to say, at least 2/3 of those are credible sources. There are thousands on google who say the same. It's you vs the world. – Wolfgang Skyler Jul 01 '13 at 02:37