CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Friend class

  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Friend class

    I would like to declare Estimator class a friend of YLC2Plot class. Any ideas??
    Thanks in advance.
    Regards: Shabbar Abbas
    i am using sun c++ 5.9 ( CC )


    *********************************************/
    #ifndef _ESTIMATOR_H
    #define _ESTIMATOR_H

    template<typename RadarType, typename FilterType>
    class IMM: public RadarType, public FilterType {
    public:
    //....
    protected:
    //...
    private:
    //..

    };

    #endif
    /******************************************/

    #ifndef _YLC2PLOT_H
    #define _YLC2PLOT_H


    #include "Estimator.h"

    class YLC2Plot : public BasePlot {
    public:
    friend IMM;

    protected:
    //....

    private:
    //.....
    };


    #endif /* _YLC2PLOT_H */
    Last edited by sza110; November 6th, 2008 at 11:53 PM. Reason: sorry, actually i want IMM class to be friend of YLC2Plot class

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Friend class

    You are doing it backwards. You have declared IMM a friend of YLC2Plot.

    The Freient declaration goes into the class that you want another class to have access to, it does NOT go into the "friend" class.

    So modifying the IMM class declaration to include Estimator as a friend is the solution.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Friend class

    Can unspecialised template classes even be friends? Which class are you specifying?

    IMM is not a class - it's a template from which the compiler can generate classes. The only way you can make it a friend is to fully specialise all the template parameters of IMM in the friend declaration, so that you have an actual class to make friends with, not a template.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Friend class

    Annoyingly friendship isn't inherited. However one workaround might be to create a YLC2Plot_Friend class declare it a friend of YLC2Plot and put a bunch of get/set methods and fuction passthoughs that access everything private. Then make the class IMM inherit from YLC2Plot_Friend.

    Its not pretty, but it will give IMM and all things that inherit from it the required functionality.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Friend class

    Quote Originally Posted by Graham View Post
    Can unspecialised template classes even be friends? Which class are you specifying?

    IMM is not a class - it's a template from which the compiler can generate classes. The only way you can make it a friend is to fully specialise all the template parameters of IMM in the friend declaration, so that you have an actual class to make friends with, not a template.
    OUCH...I missed the template...

    One alternative (which I tend to use ALOT) is to derive my template classes from a non-templated base.

    a) It clearly deliniates specialized from general aspects
    b) It *may* reduce code size
    c) You can friend other classes to the base easily...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Nov 2008
    Posts
    2

    Re: Friend class

    thankyou all for the ideas. I finally nailed it. Thnaks again

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Friend class

    Quote Originally Posted by TheCPUWizard
    It clearly deliniates specialized from general aspects ...
    ... You can friend other classes to the base easily
    here you seem to restrict friendship to the general (base) type;
    Is this intentional ? Then, do you use an accessor/mutator pattern as suggested by couling ?
    In some sense I tend to do the opposite (as in sza110 code), i.e. I friend a "secondary" class when it needs to access inherently specific parts of a "primary" class. Is this good practice ?

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Friend class

    Quote Originally Posted by superbonzo View Post
    here you seem to restrict friendship to the general (base) type;
    Is this intentional ? Then, do you use an accessor/mutator pattern as suggested by couling ?
    In some sense I tend to do the opposite (as in sza110 code), i.e. I friend a "secondary" class when it needs to access inherently specific parts of a "primary" class. Is this good practice ?
    As posted previously there are some issues with friending specializations. If the relationship is sich that it does not require access to the specialization itself (which is often the case) then my approach may make things cleaner.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Friend class

    Quote Originally Posted by Graham View Post
    Can unspecialised template classes even be friends?
    I thought they could. And in that case, all instantiations would be a friend to the class the template is declared as a friend of. The following test code compiled with Comeau online as well with VC++ 2005:
    Code:
    class SomeType
    {
         int data;
    	 public : 
    		 SomeType() : data(0){}
         template<class T>
         friend struct TemplateClass;
    };
    
    template<class T>
    struct TemplateClass
    {
         void accessAndChangePrivate(SomeType& ref)
         {
             ref.data = 10;
         }
    };
    
    int main()
    {
        TemplateClass<int> obj;
        TemplateClass<float> obj2;
        SomeType objST;
        obj.accessAndChangePrivate(objST);
        obj2.accessAndChangePrivate(objST);
    }
    The OP's syntax looks inappropriate though.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Friend class

    Quote Originally Posted by exterminator View Post
    I thought they could. And in that case, all instantiations would be a friend to the class the template is declared as a friend of. The following test code compiled with Comeau online as well with VC++ 2005[</snip>
    Here you are making all of the specializations of TemplateClass friends of the non-template class SomeType.

    I understand the issue to be more of the other way aound...

    That is to make class X a friend of a specialized templated class. In otherwords make class X a friend of TemplateClass<int>.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Friend class

    Quote Originally Posted by TheCPUWizard View Post
    Here you are making all of the specializations of TemplateClass friends of the non-template class SomeType.

    I understand the issue to be more of the other way aound...
    I don't think so, but then I am feeling sleepy now. IMM is the class template declared as friend. So, we are talking of 'friend class templates' which is exactly what I tried to put-in in the sample code.

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Friend class

    Quote Originally Posted by exterminator View Post
    I don't think so, but then I am feeling sleepy now. IMM is the class template declared as friend. So, we are talking of 'friend class templates' which is exactly what I tried to put-in in the sample code.
    The original post contains a conflict (reversal) between the TEXT and the CODE.....

    GoodNight
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Tags for this Thread

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