943,793 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 788
  • C++ RSS
Oct 7th, 2008
0

acessing a vector outside its class

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. ...
  2. vector<Pessoa*> pessoas;
  3. ...

Equipa.cpp
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 7th, 2008
0

Re: acessing a vector outside its class

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 8th, 2008
0

Re: acessing a vector outside its class

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 8th, 2008
0

Re: acessing a vector outside its class

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;
....
};
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 8th, 2008
1

Re: acessing a vector outside its class

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 8th, 2008
0

Re: acessing a vector outside its class

Click to Expand / Collapse  Quote originally posted by Narue ...
>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!
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: list of pointers; does the default destructor deallocate?
Next Thread in C++ Forum Timeline: Hello. Need help with a class problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC