mercredi 4 mars 2015

How do I return an array of pointers in a function?


So I have an array of pointers that references 3 instances of a class, I need to create a function that gets the references to those 3 instances and returns it into that array.


Here is what I have been trying:



#include<cstdlib>
#include<cinttypes>
#include<random>

//Random number generator
uint8_t rand(uint8_t max){
std::default_random_engine generator;
std::uniform_int_distribution<uint8_t> distribution(0,max);

return distribution(generator);
}

class MyClass{
//...
}
myClass[100];

MyClass * getReferences(){ //What should the type of this be?
MyClass * arrayOfPointers[3];

for(uint8_t i=0;i<2;++i){
arrayOfPointers[i]=&myClass[rand(2)];
}

return arrayOfPointers;
}

int main(){
MyClass * arrayOfPointers[3]=getReferences();

return EXIT_SUCCESS;
}



Aucun commentaire:

Enregistrer un commentaire