Are friend functions the correct answer here?

This is a discussion on Are friend functions the correct answer here? within the C++ Programming forums, part of the General Programming Boards category; Okay the idea here is that I should have a central method in the class for loading files, say, which ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Question Are friend functions the correct answer here?

    Okay the idea here is that I should have a central method in the class for loading files, say, which will auto detect the file format then call another function to actually load the data based on the format.

    I want to avoid including too many API headers in my main class source files, and I want it to be fairly extensible.

    Right now what seems to be the way to go would be something like this:

    DataObject.h
    Code:
    // etc..
    #include "DataObject_DAT.h"
    class DataObject
    {
    public:
        DataObject() : mData(0) {}
        ~DataObject() { if(mData){delete mData;mData=0;} }
        bool LoadFromFile( char const * filename );
    private:
        friend bool DataUtils::LoadFromFileDAT( char const * filename );
        char * mData;
    };
    DataObject.cpp
    Code:
    #include "DataObject.h"
    
    bool DataObject::LoadFromFile( char const * filename )
    {
        // determine file format
        if( format == eFORMAT_DAT )
            return DataUtils::LoadFromDATFile( filename );
    }
    DataObject_DAT.h
    Code:
    // etc..
    namespace DataUtils
    {
        bool LoadFromDATFile( char const * filename );
    };
    DataObject_DAT.cpp
    Code:
    #include "DataObject_DAT.h"
    #include <datfilesdk/datfilesdk.h>
    
    namespace DataUtils
    {
        bool LoadFromDATFile( char const * filename )
        {
            datfileobject datfile;
            // etc...
            datfile.readData( (void**)&mData );
            return (mData != NULL);
        }
    };
    What I like about this is that it keeps all of the "dat file sdk" out of the scope of DataObject.h which will be included anywhere that a DataObject is needed. This is nothing real at the moment, just a design idea compressed to not be too verbose.

    Is this a reasonable thing to do? Is there a better way?
    Last edited by JimFromTexas; 04-08-2010 at 03:41 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Why not just pass mData to the various data loading functions? Speaking of mData, mData should probably be a std::vector<char>, which would help you avoid silly mistakes like using delete mData instead of delete[] mData.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Hrm I think I am overthinking this. I can't see what I'm gaining by using friend rather than just declaring it as a member function and implementing it in its own cpp file..

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Why not just pass mData to the various data loading functions?
    The answer to that is the example above is simplified and there will be many member variables that would need to be passed to each function, and they'll be setting flags within the class as well. Stuff that I want to keep private except in this one case where the data is being loaded from a file. The basic idea is I only want the user-side of this to create an object of type DataObject and that file loading should be format agnostic in that scope.
    Last edited by JimFromTexas; 04-08-2010 at 03:52 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    They do not necessarily even have to be member functions. For example, you could define them as functions in an anonymous namespace in DataObject.cpp, then pass the std::vector<char> mData by const reference to each of these helper functions.

    EDIT:
    Quote Originally Posted by JimFromTexas
    The answer to that is the example above is simplified and there will be many member variables that would need to be passed to each function, and they'll be setting flags within the class as well. Stuff that I want to keep private except in this on case where the data is being loaded from a file.
    In that case, private member functions are okay.

    By the way, another thing: although you will not need to use delete and delete[] if you take my suggestion of using std::vector<char>, for those times when you do use delete or delete[], note that there is no need to check for a null pointer.
    Last edited by laserlight; 04-08-2010 at 03:54 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Friend, IMO, should only be used when overloading insertion and extraction operators (so they can access class data members) and/or to restrict, not open up, an interface.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Correct model? Virtual functions.
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 06-25-2007, 11:47 AM
  2. Problem with friend functions in templated linked list
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 03:24 PM
  3. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  4. code help :)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-28-2002, 12:12 PM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21