473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private member accessors methods for objects

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

Feb 28 '06 #1
2 2041
Alan Ning wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Feb 28 '06 #2
Alan Ning <al*******@gmail.com> wrote:
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.

--
Marcus Kwok
Mar 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3302
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
3
21369
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
12
5722
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
12
2648
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
10
1849
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
112
13725
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
8
5722
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
8
1621
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
5
1756
by: psp | last post by:
I'm really puzzled why this is allowed by the c++ compiler: class C { public: void setNumber( int y ) { m_y = y; } void printNumber() const { cout << "m_y= " << m_y << endl; } void copyInt(C...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.