c++ - Can a function doing nothing change an object? -


why function doing nothing change object?

i have following code:

void sample(slist a) {     cout << "\nin function";     a.print(); }  int main() {     srand(500);     int total = 10;     slist llist;      for(int = 0; < total; i++)     {         llist.add_head(rand() % 100);     }      llist.print();     llist.print();      sample(llist);     llist.print();     return 0; } 

the output is:

70  69  14  3   18  71  70  17  57  98   70  69  14  3   18  71  70  17  57  98   in function 70  69  14  3   18  71  70  17  57  98   0   34365728    34365696    34365664    34365632    34365600    34365568    34365536    34365504    34365472 

my question function sample has nothing related slist, changing slist. how doing that? slist singly linked list.

i think pass-by-value. great if shows me point ignorantly missing.

edit: answer title question yes. please @ answer.

look @ slist's copy constructor , destructor. guess without seeing them copy constructor makes shallow copy of data , destructor deletes data. problem copy constructor called when sample called , destructor called when sample exits. there several ways fix this.

you can pass slist reference. prevent copy constructor , destructor being called.

you can deep copy data in copy constructor.

another solution have slist store data shared_ptr rather raw data.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -