Thread: Name conflicts

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    Name conflicts

    Hello.

    I'm exporting some functions with idl. Is there a way I can define a function that has the same name as a previously defined function? For example, I want to expose a "sin" function. The problem is that the idl generates a header file and that header #includes a lot of stuff ... so can I do something about it?

    Also, I'd like some COM classes have member functions with names that are already defined elsewhere.

    Thanks.
    Last edited by Helix; 02-20-2004 at 04:40 AM.

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Is there a way I can define a function that has the same name as a previously defined function
    No there would be an ambiguous error. But you could define your functions with the same name but in a different namespace.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    No there would be an ambiguous error.
    This is true if the two functions differ only in return type. Otherwise, a function with the same name can be overloaded in C++. However, using a namespace would be a good preventative measure. Overloading two functions from different libraries without placing them in separate namespaces can cause confusion, even if there are no ambiguities in practice.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    A different namespace ...
    So how do I do that? A namespace groups some functions together or some classes or what?

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Code:
    namespace MyNamespace {
        // Stuff goes here. Functions, declarations, types, classes, etc...
    }
    As a quick example, the following code defines a simple treap type within the Ed namespace. Appologies if there are any errors, I wrote it in haste.
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using std::cout;
    using std::endl;
    
    namespace Ed {
        class Treap {
        private:
            struct node {
                int content;
                int priority;
                node *left;
                node *right;
                node(int init)
                    : content(init)
                    , priority(std::rand())
                { left = right = 0; }
            };
        public:
            Treap()
                : root(0)
                , sz(0)
            {};
            ~Treap() { destroy(root); }
            void insert(int item) { root = r_insert(root, item); }
            void remove(int item) { root = r_remove(root, item); }
            node *search(int item) { return r_search(root, item); }
            void structure() { r_structure(root, 0); }
        private:
            node *r_insert(node* curr, int item);
            node *r_remove(node *curr, int item);
            node *r_search(node *curr, int item);
            void r_structure(node *curr, int height);
            void destroy(node *curr);
        private:
            node *root;
            int sz;
        };
    
        Treap::node *Treap::r_insert(node* curr, int item)
        {
            if (curr == 0)
                curr = new node(item);
            else if (item < curr->content) {
                curr->left = r_insert(curr->left, item);
                if (curr->priority < curr->left->priority) {
                    node *temp = curr->left;
                    curr->left = temp->right;
                    temp->right = curr;
                    curr = temp;
                }
            }
            else if (item > curr->content) {
                curr->right = r_insert(curr->right, item);
                if (curr->priority < curr->right->priority) {
                    node *temp = curr->right;
                    curr->right = temp->left;
                    temp->left = curr;
                    curr = temp;
                }
            }
    
            return curr;
        }
    
        Treap::node *Treap::r_remove(node *curr, int item)
        {
            return root;
        }
    
        Treap::node *Treap::r_search(node *curr, int item)
        {
            if (curr == 0)
                return 0;
            else if (item < curr->content)
                return r_search(curr->left, item);
            else if (item > curr->content)
                return r_search(curr->right, item);
            else
                return curr;
        }
    
        void Treap::r_structure(node *curr, int height)
        {
            if (curr == 0) {
                for (int n = 0; n < height; n++)
                    cout<<'\t';
                cout<<'*'<<endl;
            }
            else {
                r_structure(curr->right, height + 1);
                for (int n = 0; n < height; n++)
                    cout<<'\t';
                cout<< curr->content <<endl;
                r_structure(curr->left, height + 1);
            }
        }
    
        void Treap::destroy(node *curr)
        {
            if (curr == 0)
                return;
            destroy(curr->left);
            destroy(curr->right);
            delete curr;
        }
    }
    
    int main()
    {
        Ed::Treap t;
        int c;
    
        while (std::cin>> c) {
            t.insert(c);
            t.structure();
            cout<<"\n==============\n";
        }
    }
    One nice feature of namespaces is that if I wanted to later add another type to the Ed namespace, I would not have to edit this code.
    Code:
    namespace Ed {
        // Stuff
    }
    
    namespace Ed {
        // More stuff
    }
    Both Stuff and More stuff are a part of the Ed namespace even though there are two declarations of said namespace.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    Thanks. I needed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to resolve conflicts in SVN?
    By jutirain in forum Tech Board
    Replies: 0
    Last Post: 01-19-2008, 10:51 PM
  2. Replies: 16
    Last Post: 01-17-2008, 04:20 PM
  3. name conflicts in enum
    By George2 in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 03:38 AM
  4. name conflicts in C
    By confuted in forum C Programming
    Replies: 12
    Last Post: 07-11-2005, 06:28 PM
  5. Class Name Conflicts....?
    By dug in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2004, 12:28 PM