CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: const Class& class

    Quote Originally Posted by kempofighter
    Actually i don't think that this is necessary since the
    // function uses Matrix's public interface but this is the pattern I see
    // when studying existing code. I guess that this is done so that the
    // function definition can directly access private/protected members.
    That is a good observation. The rule of thumb is to prefer non-member non-friend functions: GotW #84: Monoliths "Unstrung".
    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

  2. #17
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: const Class& class

    The OP defined the function within the class which is incorrect. It needs to be declared as a friend but defined globally.
    That's not actually true. Friend functions can be defined within the class and, in the case of templates, can make the code easier to write. I had such a scenario where I was overloading various operators for a templated class (+-<). Defining the operators outside of the class caused problems that required extra code to get the normal operator functionality (I can't remember what the code was exactly). Defining the functions within the class was much simpler.

    EDIT : I've been trying to remember how to reproduce the problem, but it was a long time ago.
    Last edited by JohnW@Wessex; January 16th, 2009 at 04:08 AM.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #18
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: const Class& class

    Quote Originally Posted by JohnW@Wessex View Post
    That's not actually true. Friend functions can be defined within the class...
    I know I'm stating the obvious, and I'm certainly not disagreeing with you, but although the standard specifies you can do this, you can only do it if the compiler supports it, and some don't. For this reason, and it is my preference, I tend to separate the declaration and definition.

    Also, I would have thought that for templated classes, the friend function should really have it's own template definition and not assume to inherit the class one. The reason being that if the friend function is extracted out of the class, it will need a template definition that matches that of the declaration, and that, as far as I am aware, needs to be an explicit template definition, and not one that is implied from the class template definition.

    For example, the following does not compile, because, due to the lack of a function template definition on the function declaration it fails to associate the friend declaration with global definition.
    Code:
    template<typename T>
    class MyClass
    {
    public:
      MyClass(T val)
        :val_(val)
        ,name_("MyClass") 
      {}
    
      friend std::ostream& operator<<(std::ostream& o, const MyClass<T>& m);
    
    private:
      T val_;
      std::string name_;
    };
    
    template<typename T>
    std::ostream& operator<<(std::ostream& o, const MyClass<T>& m)
    {
      o << m.name_ << " holds the value " << m.val_ << std::endl;
      return o;
    }
    Where as adding an explicit function template definition fixes the association issue...
    Code:
    template<typename T>
    class MyClass
    {
    public:
      MyClass(T val)
        :val_(val)
        ,name_("MyClass") 
      {}
    
      template<typename U>
      friend std::ostream& operator<<(std::ostream& o, const MyClass<U>& m);
    
    private:
      T val_;
      std::string name_;
    };
    
    template<typename U>
    std::ostream& operator<<(std::ostream& o, const MyClass<U>& m)
    {
      o << m.name_ << " holds the value " << m.val_ << std::endl;
      return o;
    }
    Now I realise that when you define a template function in the class, its declaration is also its definition and therefore there are no problems associating the two, but somehow:

    Code:
     template<typename T>
     class MyClass
     {
     public:
       MyClass(T val)
         :val_(val)
         ,name_("MyClass") 
       {}
     
       friend std::ostream& operator<<(std::ostream& o, const MyClass<T>& m)
       {
          o << m.name_ << " holds the value " << m.val_ << std::endl;
          return o;
       }
     
     private:
       T val_;
       std::string name_;
     };
    doesn't feel right - but I can't put my finger on the reason why. If I was forced to write the definition inside the class, I would much prefer to write:

    Code:
      template<typename T>
      class MyClass
      {
      public:
        MyClass(T val)
          :val_(val)
          ,name_("MyClass") 
        {}
      
        template<typename U>
        friend std::ostream& operator<<(std::ostream& o, const MyClass<U>& m)
        {
           o << m.name_ << " holds the value " << m.val_ << std::endl;
           return o;
        }
      
      private:
        T val_;
        std::string name_;
      };
    Last edited by PredicateNormative; January 16th, 2009 at 04:53 AM.

  4. #19
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: const Class& class

    Quote Originally Posted by laserlight View Post
    That is a good observation
    A very good observation... and I completely missed it.

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

    Re: const Class& class

    Quote Originally Posted by PredicateNormative View Post
    I know I'm stating the obvious, and I'm certainly not disagreeing with you, but although the standard specifies you can do this, you can only do it if the compiler supports it, and some don't.
    Am I missing something??

    If the compiler states something is legal, and the compiler will not allow it, isn't that prima facia evidence that you have a non-conformant compiler????

    (I admit I have not checked the wording of the specificiation)
    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. #21
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: const Class& class

    No you're not missing anything, you are correct, the result is that if it isn't supported that the compiler is non-standard.

Page 2 of 2 FirstFirst 12

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