Say i want a friend function to have access to two classes which are derived from the same base..I declare the friend function prototye in both classes, but where do i code the actual definition?? is the code below right?? (mixBreed() is the friend function)..

// Dog.h

#include "Animal.h"

#ifndef Dog_h
#define Dog_h

using namespace std;

class Dog{
      protected:
        int  ID;
      
      public:
        int getID(){return ID;}    //17. Inline methods..
        void setID(int newID){ID=newID;}

 };
  
 class Retriever; 
 class Alsation: public Dog{
       private:
         int height, weight; 
       
       public:
         Alsation(int aHeight, int aWeight); 
         Alsation(){};    
         friend void mixBreed(const Alsation &, const Retriever &);  //Prototype
         Alsation operator+ (Alsation x);
         Alsation Alsation::operator= (const Alsation &y);
         bool Alsation::operator == (Alsation x);
         void display(); 
       };  

       
  class Retriever: public Dog{
       private:
         int height, weight; 
       
       public:
         Retriever(int aHeight, int aWeight);     
         friend void mixBreed(const Alsation &, const Retriever &);  //Prototype
       };

         void mixBreed(const Alsation &x, const Retriever &y){
            cout << x.height + y.height << endl;}     //Friend function definition
       
#endif

Recommended Answers

All 6 Replies

I see one potential problem here. You have a forward declaration class Retriever..Which makes me wonder??? If you have a Alsation object and call your friend function which has a reference to a Retriever how do you intend to handle the fact that your data members may deficient for a Retriever?

It seems to me that this function would make more sense as a non-friend/non-member function defined after both Retriever and Alsation.

I see no syntax problem with this code.. have you tried compiling it?

The only problem is that you are using friendship relation for a pretty trivial purpose. What is wrong with a simple getHeight() member function in both classes, or better yet as a virtual method in the Dog class. The problem with your code is mainly that what if you have a third, fourth, fifth, .. tenth breed of dog.. how many versions of mixBreed() will you have to implement. You can see that it is not an easily scalable design (mainly because you introduce a coupling between the classes Alsatian and Retriever when these classes are not related (the Alsatian breed of dog has no need for the Retriever breed of dog to exist), don't introduce dependencies that don't make sense, they will haunt you later).

Thanks for replies guys..

mike
Im only doing it because we have to demonstrate use of it in an assignment..ive changed the argument types from constant reference to reference and am getting the following error:

main1.o(.text+0x1bf):main1.cpp: undefined reference to `mixBreed(Alsation const&, Retriever const&)'
collect2: ld returned 1 exit status
make.exe: *** [AnimalAssign.exe] Error 1

Any ideas?
Thanks

You have to make sure that the mixBreed() function is always declared with the same prototype. And in this case, const makes sense and it probably should stay.

if the idea of a friend function is the ability to have access to its friends private members what is the point of sending the object by constant ref??

Thanks

The const keyword only controls whether you can modify the object or not. There are two kinds of access: read and write. Const just forbids the write access but, with friendship, you still have read access to private members. Generally, if a function does not need to modify the object(s), it should have const qualifiers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.