C++ Object references in loop cycle

后端 未结 3 340
清歌不尽
清歌不尽 2021-01-27 11:49

I\'m trying to create different objects of the same type using a loop, and then storing a pointer to each specific object in a linked list. The problem is, each time an object i

相关标签:
3条回答
  • 2021-01-27 12:14

    This line

    cout<<&user<<endl;
    

    prints the address of a pointer to an object. user is itself a pointer to the object you're creating. To print the address of your object, you meant to write

    cout<<user<<endl;
    

    Although it'll be a new object each time, the variable user is always in the same place. You can add the value of user to your list, and it will indeed be different each time.

    0 讨论(0)
  • 2021-01-27 12:21
    cout<<&user<<endl;
    

    should be:

    cout<<user<<endl;
    

    &user is address of local variable Utilizador*, which remains the same. user variable value itself is the pointer you need, and it should be different on every iteration.

    0 讨论(0)
  • 2021-01-27 12:21

    "&user" return address of pointer, that contains references to objects.

    0 讨论(0)
提交回复
热议问题