Classes, constructor and pointer class members

Posted by pocoa on Stack Overflow See other posts from Stack Overflow or by pocoa
Published on 2010-04-02T05:01:14Z Indexed on 2010/04/02 5:03 UTC
Read the original article Hit count: 373

Filed under:
|
|

I'm a bit confused about the object references. Please check the examples below:

class ListHandler {
public:
   ListHandler(vector<int> &list);
private:
   vector<int> list;
}

ListHandler::ListHandler(vector<int> &list) {
   this->list = list;
}

Here I would be wasting memory right? So the right one would be:

class ListHandler {
public:
   ListHandler(vector<int>* list);
private:
   vector<int>* list;
}

ListHandler::ListHandler(vector<int>* list) {
   this->list = list;
}

ListHandler::~ListHandler() {
   delete list;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about class