lundi 25 juillet 2016

'auto' difference between pointers and references [duplicate]

This question already has an answer here:

Given this code:

int& some_class::ret_ref(){
    return this->some_integer;
}
int* some_class::ret_ptr(){
    return &(this->some_integer);
}

int main(){
    some_class c;
    auto p=c.ret_ptr();
    auto r=c.ret_ref();
}

At compile time:

  • p is int*
  • r is int

Why was auto deduced as int* in p and it was not deduced as int& in r?

What is the rule here?

Aucun commentaire:

Enregistrer un commentaire