acessing a vector outside its class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

acessing a vector outside its class

 
0
  #1
Oct 7th, 2008
Hi,

I'm doing a program that has a class called Liga. That class has a vector with 'Equipa's.

Liga has a vector called 'pessoas' which I want to access inside a function in each Equipa, is this possible?

for example:

Liga.h
  1. ...
  2. vector<Pessoa*> pessoas;
  3. ...

Equipa.cpp
  1. ...
  2. void Equipa::mostraStaff()
  3. {<blockquote>for(unsigned int i=0;i!=pessoas.size();i++)<blockquote>pessoas[i].mostra();</blockquote></blockquote>}
  4. ...

thanks in advance!
Last edited by onemanclapping; Oct 7th, 2008 at 10:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: acessing a vector outside its class

 
0
  #2
Oct 7th, 2008
Well you have a few options.
1. You can make pessoas public
2. You can create a function called getPessoas() which will return a pointer to pessoas for that particular Liga object.
3. You can make Equipa a friend of Liga, that way it can access its data members.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: acessing a vector outside its class

 
0
  #3
Oct 8th, 2008
Originally Posted by stilllearning View Post
You can make pessoas public.
that's good! I just need to declare it as public and then include "Liga.h" in "Equipa.h", right?

as for declaring Equipa a friend of Liga, how can I do that?

thanks again!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: acessing a vector outside its class

 
0
  #4
Oct 8th, 2008
Yes you can make it public and add the header to Equipa. If you want only Equipa to access the data members of Liga, and not any other class, then you can declare it as a friend:

class Liga {
  friend class Equipa;
....
};
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: acessing a vector outside its class

 
1
  #5
Oct 8th, 2008
>that's good! I just need to declare it as public
It's rather depressing how you latched on to the worst possible option that stilllearning suggested.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: acessing a vector outside its class

 
0
  #6
Oct 8th, 2008
Originally Posted by Narue View Post
>that's good! I just need to declare it as public
It's rather depressing how you latched on to the worst possible option that stilllearning suggested.
actually I didn't
I modified my primary algorithm and now I don't need to do that anymore

thanks for all your help, though!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC