943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 500
  • C++ RSS
Mar 29th, 2009
0

Classes

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opposition is offline Offline
17 posts
since Sep 2008
Mar 29th, 2009
0

Re: Classes

I think what you want is a friend class. I've never used it, but I bet google knows about it
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 29th, 2009
0

Re: Classes

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
opposition is offline Offline
17 posts
since Sep 2008
Mar 29th, 2009
0

Re: Classes

> 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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 29th, 2009
0

Re: Classes

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 29th, 2009
0

Re: Classes

It seems it depends which order you declare the classes matters? For example, this does not compile:

C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 30th, 2009
0

Re: Classes

Click to Expand / Collapse  Quote originally posted by daviddoria ...
For example, this does not compile:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 30th, 2009
0

Re: Classes

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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 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: How to store the data to afile int this problem .
Next Thread in C++ Forum Timeline: Help with error C2228:





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


Follow us on Twitter


© 2011 DaniWeb® LLC