Click to See Complete Forum and Search --> : method between classes
ulumulu
January 29th, 2008, 11:08 AM
Good day.
Lets say I have Class A and Class B. Class A have method DoIt() and some variables. And I want that this method DoIt() would be availible at Class B, but only method no variables.
So:
* friend functions wont work there, because it will give me access to var.
* inheritance the same as first.
* static is bad either, because it will be availible to all other classes.
Any ideas how to implement that ?
Thanks for your answers
Lindley
January 29th, 2008, 11:21 AM
I'm not sure if "friend chaining" works. If not, then you could do:
Create a class C which is a friend of A, but only has a routine to call that one function from A. Make B a friend of C.
Then B is the only class to have access to C's function, and C's function is the only way for B to do anything in A.
Again, not sure what effect that sort of friend-chaining will have.
laserlight
January 29th, 2008, 11:24 AM
inheritance the same as first.
Make the member variables of A private and DoIt() protected. Of course, this does not prevent some other derived class from accessing DoIt().
That said, why do you want to do this?
ulumulu
January 29th, 2008, 11:47 AM
well it's a few mans project. and you know, sometimes one need access to the same functions which i wrote before. So I could make it friendly to his class functions,but I don't want to give him access to my other functions and variables.
Maybe there exists some kind of strong type friend functions or something ... ?
Lindley
January 29th, 2008, 11:54 AM
Do these functions manipulate or rely on class-private data?
If not, just break them out into a separate class.
If you've designed your objects well, this sort of thing shouldn't come up. What's public and what's private in a class should be obvious from the class's purpose; and if there's an algorithm that will need to be run on the private data of several classes, then that algorithm should be broken out into a third class which becomes a member of those which use it, rather than just a private function.
The purpose of classes is to abstract implementation details so that programs can be read in terms of high-level concepts. If you've got bizarre cross-linking all over the place between classes, you're rather missing the point.
laserlight
January 29th, 2008, 11:59 AM
well it's a few mans project. and you know, sometimes one need access to the same functions which i wrote before. So I could make it friendly to his class functions,but I don't want to give him access to my other functions and variables.
It sounds like you may want to create a class for your partner to use, then extend this class for your own part of the project.
ulumulu
January 29th, 2008, 12:24 PM
thank you guys for your opinions. Probably I will re-think these class implementation in different way, better then cross linking between them .
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.