Say I am given a class constructor:
.h file
class Memo {
public:
typedef vector<Memo> MemoList;
Memo (int id, Memo::MemoList &list);
MemoList pile;
};
.cc file
Memo::Memo (int id, Memo::MemoList &list) {
pile = list;
}
Would pile be the “same thing” or “equal to” list, or do I need to have a new variable which acts as a reference to list? I would like to be able to modify pile and have it affect the original list.
Simple answer to your problem:
:
You need to store the list as reference:
And reference must be initialized on initialization list of your constructor:
When your
Memowould has to be copyable (now it is not) use pointer instead of reference.And remember (maybe it should be told at first) that programming in this way is not very safe – you must be sure that your list lifetime is longer than your Memo objects!!!