Classes

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

Join Date: Sep 2008
Posts: 15
Reputation: opposition is an unknown quantity at this point 
Solved Threads: 0
opposition opposition is offline Offline
Newbie Poster

Classes

 
0
  #1
Mar 29th, 2009
Hey was just wondering if there was a way I could share the private data with another class, without including it as a object of that class, for example, i have 2 classes, class A and blass B. class 'A' reads in from a txt file and stores its data in 2 string arrays, such as string firstname[100]; string lastname[100]; which are in the private section, how would i copy this into class B?

what i want to do is basically strcpy(classB.firstname, classA.firstname);

is this possible?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Classes

 
0
  #2
Mar 29th, 2009
I think what you want is a friend class. I've never used it, but I bet google knows about it
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 15
Reputation: opposition is an unknown quantity at this point 
Solved Threads: 0
opposition opposition is offline Offline
Newbie Poster

Re: Classes

 
0
  #3
Mar 29th, 2009
thanx for your reply, Ive been looking around and havent been able to get it to work with a friends class is there any other way of doing it? i might be doing it wrong lol
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Classes

 
0
  #4
Mar 29th, 2009
> You could maybe try creating pointers in the public section of your class and point them to the private data you want to share ...

But you should definitely learn how to use a 'friend', it's a much better way to do this ...

> You could also create an extra class which holds all the data which has to be shared among the other classes ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Classes

 
0
  #5
Mar 29th, 2009
Here are some links which are explaining how to use 'friends':
http://www.codersource.net/cpp_tutorial_friend.html
http://www.cprogramming.com/tutorial/friends.html

Just remember: 'Google is your friend !'
Last edited by tux4life; Mar 29th, 2009 at 1:17 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Classes

 
0
  #6
Mar 29th, 2009
It seems it depends which order you declare the classes matters? For example, this does not compile:

  1. class Point
  2. {
  3. private:
  4. //friend class PointFriend;
  5. double x,y,z;
  6.  
  7. public:
  8. Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
  9. };
  10.  
  11. class PointFriend
  12. {
  13. private:
  14. friend class Point;
  15. public:
  16. PointFriend()
  17. {
  18. Point P(1.2, 2.3, 3.4);
  19. cout << P.x << endl;
  20. }
  21. };

But if you uncomment the "friend class PointFriend;" and comment "friend class Point", it works correctly.

Why is that?

Dave
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Classes

 
0
  #7
Mar 30th, 2009
Originally Posted by daviddoria View Post
For example, this does not compile:
  1. class Point
  2. {
  3. private:
  4. //friend class PointFriend;
  5. double x,y,z;
  6.  
  7. public:
  8. Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
  9. };
  10.  
  11. class PointFriend
  12. {
  13. private:
  14. friend class Point;
  15. public:
  16. PointFriend()
  17. {
  18. Point P(1.2, 2.3, 3.4);
  19. cout << P.x << endl;
  20. }
  21. };

But if you uncomment the "friend class PointFriend;" and comment "friend class Point", it works correctly.
PointFriend must be a friend of Point because PointFriend's constructor accesses a private member of Point (i.e. P.x).

Whether or not "friend class Point;" is declared in the above example makes no difference because class Point does not make any use of class PointFriend. If it would, then you'd have to split the code into .h and .cpp files with appropriate #includes in order to have a mutual friendship.

P.S. Note that you can also have individual member functions as friends of a class.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Classes

 
0
  #8
Mar 30th, 2009
Ah, I though I had read somewhere that the friendship was mutual - ie. if you make A a friend of B, then B is automatically a friend of A, but I guess that is just wrong, as this example shows haha.

Thanks,

Dave
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC