| | |
Classes
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 15
Reputation:
Solved Threads: 0
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?
what i want to do is basically strcpy(classB.firstname, classA.firstname);
is this possible?
> 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 ...
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."
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 !'
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."
•
•
Join Date: Feb 2008
Posts: 630
Reputation:
Solved Threads: 46
It seems it depends which order you declare the classes matters? For example, this does not compile:
But if you uncomment the "friend class PointFriend;" and comment "friend class Point", it works correctly.
Why is that?
Dave
C++ Syntax (Toggle Plain Text)
class Point { private: //friend class PointFriend; double x,y,z; public: Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {} }; class PointFriend { private: friend class Point; public: PointFriend() { Point P(1.2, 2.3, 3.4); cout << P.x << endl; } };
But if you uncomment the "friend class PointFriend;" and comment "friend class Point", it works correctly.
Why is that?
Dave
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
For example, this does not compile:
C++ Syntax (Toggle Plain Text)
class Point { private: //friend class PointFriend; double x,y,z; public: Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {} }; class PointFriend { private: friend class Point; public: PointFriend() { Point P(1.2, 2.3, 3.4); cout << P.x << endl; } };
But if you uncomment the "friend class PointFriend;" and comment "friend class Point", it works correctly.
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.
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- Loading classes from a .jar file (Java)
- Help with Classes (C++)
- Understanding classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: How to store the data to afile int this problem .
- Next Thread: Help with error C2228:
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






is there any other way of doing it? i might be doing it wrong lol
