C++ Object references in loop cycle

后端 未结 3 348
清歌不尽
清歌不尽 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<

    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<

    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.

提交回复
热议问题