lundi 9 mars 2015

Assign function Return Value to Array - could this call a copy constructor?

The following example snippets illustrate the code I am thinking about. Unfortunatelly i do not have an appropriate compiler ready here so I can not deliver a full example. (Not sure if the code below contains errors).



Package * Controller::getPackage(unsigned i)
{
//All possible values for i contain a pointer to a valid package
return this->magicPackages[i];
}

Requester::startRequest(Controller &controller)
{
const unsigned N = 10;
Package (&packages)[N];

for(unsigned i = 0; i < N; ++i)
{
packages[i] = controller.getPackage(i);
}
}



  • Will Package (&packages)[N] only take lvalue references?

  • When would this copy a Package?

  • How can i prevent an assignment to ever copy a Package?




Edit:


The comments below suggest that the Array would not compile since the references are not initialised.


I try to request a buffer of references from another object and do something with them afterwards. Just passing a pointer value around would not be problematic for me.


I thougth writing it as a reference would help the compiler when optimizing the code that is why I tried to write the array as an array of references.


The comments below are useful and i think they should be made into an answer, i would accept an answer that states why this can not be done.


Aucun commentaire:

Enregistrer un commentaire