简体   繁体   中英

How to point to array of pointers to class instances?

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?

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.

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
                                         // ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM