CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    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 ... ?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    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
  •  





Click Here to Expand Forum to Full Width

Featured