473,587 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ 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 8803
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
29513
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 reference will be handled by the calling function ? Can any one in the group point me to the correct solution for it ? Any code snippets will be of great help. Thanks in advance.
9
11902
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 up to which size m would you expect vector<double> returns_by_value() {
18
2126
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 problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
5
2267
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 foo(const std::string& rhs) { std::string var = rhs ; var += " received" ;
10
10276
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. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
13
2303
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
1863
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 want to create inside that function a matrix (i.e. int a) and than passing the reference to it I can simply returning that value: in main()
7
2399
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 value occurs in th vector. */
23
2922
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 method. QUOTE ENDS HERE I have never heard anyone else say that it is a problem for a function
0
7854
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
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.