Results 1 to 6 of 6

Thread: Friend Functions in Namespaces.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089

    Friend Functions in Namespaces.

    I'm having a bit of a declarations Problem and I can't seem to get the right order of declarations.

    I have a class, "DisplayTree" which represents various things to be drawn on an HDC.


    The DisplayTrees are Drawn using an array of functions
    DrawDisplayTreeFunctions[] which are not part of the class but are friends with it. And as I need lots of seperate functions to fill the Array I'd rather stow them all away in a namespace.

    So Far So Good, I just do this.



    Code:
    
    //Declarations of array Functions in a Namespace
    namespace nDrawDisplayTreeFunctions
    {
    
        void ArrayFunction1(void);
        void ArrayFunction2(void);
        void ArrayFunction3(void);
        void ArrayFunction4(void);
        void ArrayFunction5(void);
    
    }
    
    //Function Array With Functions In it
    void (* DrawDisplayTreeFunctions[]) (void) = 
    {
            nDrawDisplayTreeFunctions::ArrayFunction1,
            nDrawDisplayTreeFunctions::ArrayFunction2,
            nDrawDisplayTreeFunctions::ArrayFunction3,
            nDrawDisplayTreeFunctions::ArrayFunction4,
            nDrawDisplayTreeFunctions::ArrayFunction5
    };
    
    
    
    
    class DisplayTree
    {
    
    
        friend void nDrawDisplayTreeFunctions::ArrayFunction1(void);
        friend void nDrawDisplayTreeFunctions::ArrayFunction2(void);
        friend void nDrawDisplayTreeFunctions::ArrayFunction3(void);
        friend void nDrawDisplayTreeFunctions::ArrayFunction4(void);
        friend void nDrawDisplayTreeFunctions::ArrayFunction5(void);
    
    
         //Other class members
    
    };
    
    
    namespace nDrawDisplayTreeFunctions
    {
    
        void ArrayFunction1(void)
        {
    
            //Function definition
    
        }
    
    
        void ArrayFunction2(void)
        {
    
            //Function definition
    
        }
    
    
        void ArrayFunction3(void)
        {
    
            //Function definition
    
        }
    
    
        void ArrayFunction4(void)
        {
    
            //Function definition
    
        }
    
    
        void ArrayFunction5(void)
        {
    
            //Function definition
    
        }
    
    }


    And That's all Lovely The Array Functions can get at the protected members of the class and they're all tucked away in a namespace where they can't be called willy nilly.

    The trouble is I want the functions to take a *DisplayTree as a parameter. Which causes problems, because I have to Declare the Functions inside the namspace before I define the class so I can make them friends. And I have to declare the class before I declare the functions So they can take a *DisplayTree as a Parameter. Can Anyone see a way around this Problem?

    help.
    If it wasn't for this sentence I wouldn't have a signature at all.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why not put a
    Code:
    class DisplayTree;
    at the top? That will tell the compiler that there is code for that, but it can't have it just yet.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Thanks parksie.

    I didn't realise yu could do that for classes.
    If it wasn't for this sentence I wouldn't have a signature at all.

  4. #4
    amac
    Guest
    what is the advantage of using namespaces?

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    namespaces will keep your code organized, but mostly theyre good for preventing name collisions between included files.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    See the STL. For example, whenever anyone uses something from the STL, you often see "using namespace std;". You shouldn't really do that but it's okay for .cpp files. However, you should never import a whole namespace into a header file. If you use it in a source file the names are only imported for that file so you can keep track easier.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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