Private member accessors methods for objects

A

Alan Ning

I have a class that contains a stl vector as a private member. I would
like to write an accessor method for this stl vector. However, I don't
want to return a whole copy of the vector. What should I do in this
case?

Right now, I am returning the address of the vector.

const vector<> * ReturnVector() const { return m_vector; };

Is this a good idea? What's the proper way of writing an accessor
method for a big object such as the stl vector?

Thanks

.... Alan
 
P

Phlip

Alan said:
Is this a good idea? What's the proper way of writing an accessor
method for a big object such as the stl vector?

Write the calling code first (preferably as a test case). What does that
code look like? Should it work with an iterator into the vector? With an
index? Or should it tell the vector's owner what high-level thing it wants
done.

Encapsulation is not the same thing as making all data private, then
creating accessors for all the data.
 
M

Marcus Kwok

Alan Ning said:
I have a class that contains a stl vector as a private member. I would
like to write an accessor method for this stl vector. However, I don't
want to return a whole copy of the vector. What should I do in this
case?

Right now, I am returning the address of the vector.

const vector<> * ReturnVector() const { return m_vector; };

Is this a good idea? What's the proper way of writing an accessor
method for a big object such as the stl vector?

You could return a (const) reference to the vector, but read Phlip's
reply to make sure that's really what you want to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top