473,467 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning reference to vector from function

4 New Member
Hello:

What is the syntax for returning a reference to a vector from a function? I have a private vector and I want to return it using a public get function (like in the code below), but I believe that what I have will make a copy of the vector, and my real vector is several hundred bytes. Do I have to cast it as a constant? What is the syntax for that (or where does one look)?

Sorry if these are basic questions - alas, I've been scouring the internet for several days now!!!

Many thanks for any assistance you can offer!

Michele
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <vector>
  3. #include "VectorThing.h"
  4. using namespace std;
  5.  
  6. VectorThing::VectorThing()
  7. {
  8.         vect.push_back("macaroni");
  9.         vect.push_back("cheese");
  10.  
  11. }
  12.  
  13. vector <string> VectorThing::getVector()
  14. {
  15.         return vect;
  16. }
  17.  
  18. And this is my header file:
  19.  
  20. #include <string>
  21. #include <vector>
  22. using namespace std;
  23.  
  24. class VectorThing
  25. {
  26.         public:
  27.                 VectorThing();
  28.                 vector <string> getVector();
  29.  
  30.         private:
  31.                 vector <string> vect;
  32.  
  33. };
Mar 22 '07 #1
3 8778
sicarie
4,677 Recognized Expert Moderator Specialist
Please use code tags around your code - it helps greatly with readability.

Why not just declare the vector locally?
Mar 22 '07 #2
horace1
1,510 Recognized Expert Top Contributor
try using &, e.g.
Expand|Select|Wrap|Line Numbers
  1. vector <string>  & VectorThing::getVector()
  2. {
  3.         return vect;
  4. }
  5.  
but this will give read/write access to the vector from outside the class, e.g.
Expand|Select|Wrap|Line Numbers
  1.     vector<string> &v1=v.getVector();
  2.     cout << v1[0] << endl;
  3.     v1[1]="salad";
  4.  
You could return a constant reference
Expand|Select|Wrap|Line Numbers
  1. const vector <string>  & VectorThing::getVector()
  2. {
  3.         return vect;
  4. }
  5.  
which would give read access but not write
Mar 22 '07 #3
Michele
4 New Member
Thanks!!! That is exactly what I was looking for. I was trying some version of that but I thought I had to cast the vector to return it as a constant. But as it compiles I see now that I don't!

Thanks!
Michele
Mar 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: tornado | last post by:
hi all, i am pretty new to c++. i have this problem for which i am unable to think a solution. i don't understand how to pass a vector refernce back to the callin function. And how this...
9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
5
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
13
by: Gernot Frisch | last post by:
Which method is the fastest/best: std::vector<intfoo1() { std::vector<intv; ... reutun v; } std::vector<int>& foo2()
9
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I...
7
by: arnuld | last post by:
/* C++ Primer - 4/e * * 1st example from section 7.2.2, page 234 * returning 2 values from a function * * STATEMENT: * to find a specific value in a vector and number of times * that...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
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,...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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 ...

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.