Interesting behavior with explicit copy constructor

This is a discussion on Interesting behavior with explicit copy constructor within the C++ Programming forums, part of the General Programming Boards category; I came across some weird behavior when trying to use the explicit keyword on a copy constructor. Take the example ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    90

    Interesting behavior with explicit copy constructor

    I came across some weird behavior when trying to use the explicit keyword on a copy constructor. Take the example code below:

    Code:
    class myint {
        int x;
    public:
        myint();
        explicit myint(int i);
        explicit myint(const myint &i);
        myint &operator=(int i);
        myint &operator=(const myint &i);
        const myint operator+(int i);
        const myint operator+(const myint &i);
        const myint operator-(int i);
        const myint operator-(const myint &i);
        myint &operator+=(int i);
        myint &operator+=(const myint &i);
        myint &operator-=(int i);
        myint &operator-=(const myint &i);
    };
    
    myint::myint()
    {
        x=0;
    }
    
    myint::myint(int i)
    {
        x=i;
    }
    
    myint::myint(const myint &i)
    {
        x=i.x;
    }
    
    myint &myint::operator=(int i)
    {
        x=i;
        return *this;
    }
    
    myint &myint::operator=(const myint &i)
    {
        x=i.x;
        return *this;
    }
    
    const myint myint::operator+(int i)
    {
        return myint(x+i);
    }
    
    const myint myint::operator+(const myint &i)
    {
        return myint(x+i.x);
    }
    
    const myint myint::operator-(int i)
    {
        return myint(x-i);
    }
    
    const myint myint::operator-(const myint &i)
    {
        return myint(x-i.x);
    }
    
    myint &myint::operator+=(int i)
    {
        return *this=*this+i;
    }
    
    myint &myint::operator+=(const myint &i)
    {
        return *this=*this+i;
    }
    
    myint &myint::operator-=(int i)
    {
        return *this=*this-i;
    }
    
    myint &myint::operator-=(const myint &i)
    {
        return *this=*this-i;
    }
    
    int main()
    {
        myint a,b(3);
        myint c(b);
        myint *d = new myint(4);
    
        a=b+c;
        a=b+0;
        delete d;
        return 0;
    }
    This compiles fine with Sun's compiler (CC: Sun C++ 5.8 2005/10/13). But if I try to compile with the GNU compiler (3.4.6 and 4.1.2), it gives me this:

    Code:
    x5.cpp: In member function `const myint myint::operator+(int)':
    x5.cpp:50: error: no matching function for call to `myint::myint(myint)'
    x5.cpp: In member function `const myint myint::operator+(const myint&)':
    x5.cpp:55: error: no matching function for call to `myint::myint(myint)'
    x5.cpp: In member function `const myint myint::operator-(int)':
    x5.cpp:60: error: no matching function for call to `myint::myint(myint)'
    x5.cpp: In member function `const myint myint::operator-(const myint&)':
    x5.cpp:65: error: no matching function for call to `myint::myint(myint)'
    x5.cpp: In member function `myint& myint::operator+=(int)':
    x5.cpp:70: error: no matching function for call to `myint::myint(const myint)'
    x5.cpp: In member function `myint& myint::operator+=(const myint&)':
    x5.cpp:75: error: no matching function for call to `myint::myint(const myint)'
    x5.cpp: In member function `myint& myint::operator-=(int)':
    x5.cpp:80: error: no matching function for call to `myint::myint(const myint)'
    x5.cpp: In member function `myint& myint::operator-=(const myint&)':
    x5.cpp:85: error: no matching function for call to `myint::myint(const myint)'
    x5.cpp: In function `int main()':
    x5.cpp:94: error: no matching function for call to `myint::myint(const myint)'
    x5.cpp:95: error: no matching function for call to `myint::myint(const myint)'
    I tried removing "explicit" from the copy constructor and stepping through the code in gdb, but it does seem to call this function at all at these points.

    Any idea what's going on here? You would think something like this wouldn't differ between compilers.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explicit constructor on strings
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2006, 03:46 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM
  4. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 06:17 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 04:04 PM

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