|
-
January 29th, 2008, 12:08 PM
#1
method between classes
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
-
January 29th, 2008, 12:21 PM
#2
Re: method between classes
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.
-
January 29th, 2008, 12:24 PM
#3
Re: method between classes
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?
-
January 29th, 2008, 12:47 PM
#4
Re: method between classes
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 ... ?
-
January 29th, 2008, 12:54 PM
#5
Re: method between classes
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.
Last edited by Lindley; January 29th, 2008 at 12:59 PM.
-
January 29th, 2008, 12:59 PM
#6
Re: method between classes
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.
-
January 29th, 2008, 01:24 PM
#7
Re: method between classes
thank you guys for your opinions. Probably I will re-think these class implementation in different way, better then cross linking between them .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|