Thread: trouble with friend functions/classes

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    trouble with friend functions/classes

    ok i have a class called CBook...some of the code goes like this


    Code:
    class CBook
    {
    public:
    CBook() {m_Name = new char[10];}
    void SetName(char* name);
    ~CBook() { delete [] m_Name; }
    private:
    char* m_Name;
    
    friend char* GetName(CBook aBook);
    };
    
    void CBook::SetName(char* name)
    {
    delete [] m_Name;
    m_Name = new char[strlen(name) + 1];
    strcpy(m_Name, name);
    }
    
    char* GetName(CBook aBook)
    {
    return aBook.m_Name;
    }
    after i instantiate an object of CBook named myBook and passed it to the friend function GetName(myBook); it says some memory error...what is wrong...i dont get it...but if i make the function part of the class CBook and call GetName() it works perfectly..is there some different with friend functions...thanks

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because you are passing the object and not a REFERENCE to the object. Since you're passing the object and not it's location in memory, your system has to make a copy of the object. Since you didn't define a copy constructor, it has to use the default copy constructor which is bad because you are using dynamic memory allocation, so what happens is, the local object's m_Name pointer gets the value of the object being passed's m_Name pointer, but then when the function ends, the local object's destructor is called, in turn deallocating the memory that both the local object and the object being passed share. So now, anytime you use the other object after the function call you are dealing with deallocated memory, which is very very bad.

    So again, you're best off making the function take a reference instead of an object (here it doesnt really make sense to use a friend function at all, to be honest, but I have a feeling you're just using it to test out friend functions), otherwise you will, in one way or another, be working with deallocated memory.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Try indenting your code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 04:44 PM
  2. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  3. Problem with friend functions in templated linked list
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 04:24 PM
  4. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 10:53 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM