multiple copy constructors

Discussion in 'C++' started by Glen Able, Feb 19, 2004.

  1. Glen Able

    Glen Able Guest

    Any ideas why compilers seem to give warnings about multiple copy
    constructors e.g.

    class A
    {
    public:
    A(A& a);
    A(const A& a);
    };


    Also, are there any other forms which would constitute a copy ctor?

    thanks.
    Glen Able, Feb 19, 2004
    #1
    1. Advertising

  2. Glen Able

    David Rubin Guest

    Glen Able wrote:

    > Any ideas why compilers seem to give warnings about multiple copy
    > constructors e.g.
    >
    > class A
    > {
    > public:
    > A(A& a);
    > A(const A& a);
    > };


    The problem is that a two functions cannot be differentiated solely on the basis
    of the 'constness' of their arguments.

    /david

    --
    Andre, a simple peasant, had only one thing on his mind as he crept
    along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    -- unknown
    David Rubin, Feb 19, 2004
    #2
    1. Advertising

  3. David Rubin wrote:

    > Glen Able wrote:
    >
    >> Any ideas why compilers seem to give warnings about multiple copy
    >> constructors e.g.
    >>
    >> class A
    >> {
    >> public:
    >> A(A& a);
    >> A(const A& a);
    >> };

    >
    > The problem is that a two functions cannot be differentiated solely on
    > the basis of the 'constness' of their arguments.


    That's right. Furthermore, IMHO, it's non sens to pass a non constant
    parameter to a copy constructor, since the copy constructor's role is to
    only make a *copy* of the passed parameter.

    So there is absolutely no reason to have a constructor like this:

    A(A& a)

    --
    Guillaume Brocker
    Guillaume Brocker, Feb 19, 2004
    #3
  4. >
    > That's right. Furthermore, IMHO, it's non sens to pass a non constant
    > parameter to a copy constructor, since the copy constructor's role is to
    > only make a *copy* of the passed parameter.
    >
    > So there is absolutely no reason to have a constructor like this:
    >
    > A(A& a)
    >


    std::auto_ptr has a copy constructor exactly like that.

    It's not common, but it is legal and very occasionally useful.

    john
    John Harrison, Feb 19, 2004
    #4
  5. David Rubin wrote in news:c12p1g$:

    > Glen Able wrote:
    >
    >> Any ideas why compilers seem to give warnings about multiple copy
    >> constructors e.g.
    >>
    >> class A
    >> {
    >> public:
    >> A(A& a);
    >> A(const A& a);
    >> };

    >
    > The problem is that a two functions cannot be differentiated solely on
    > the basis of the 'constness' of their arguments.


    No, all the usual overload resolution rules apply:

    #include <iostream>

    struct X
    {
    X() { std::cerr << "X::X()\n";}
    X( X const & ) { std::cerr << "X::X( X const & )\n"; }
    X( X & ) { std::cerr << "X::X( X & )\n"; }
    };


    X f() { return X(); }


    int main()
    {
    using namespace std;
    X const x = f();
    X y( x );
    X z( y );
    }

    I get:
    X::X()
    X::X( X const & )
    X::X( X & )

    With all 5 compilers I tried, for some reason VC 7.1 gives
    the warning descibed by the OP. I can't think of a good reason
    for it, but VC 7.1 has a whole bunch of backward compatability
    features that often don't make sense, so maybe thats it.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Feb 20, 2004
    #5
  6. Glen Able

    David Rubin Guest

    Rob Williscroft wrote:

    > David Rubin wrote in news:c12p1g$:
    >
    >
    >>Glen Able wrote:
    >>
    >>
    >>>Any ideas why compilers seem to give warnings about multiple copy
    >>>constructors e.g.
    >>>
    >>>class A
    >>>{
    >>>public:
    >>> A(A& a);
    >>> A(const A& a);
    >>>};

    >>
    >>The problem is that a two functions cannot be differentiated solely on
    >>the basis of the 'constness' of their arguments.

    >
    >
    > No, all the usual overload resolution rules apply:


    [snip]
    > With all 5 compilers I tried, for some reason VC 7.1 gives
    > the warning descibed by the OP. I can't think of a good reason
    > for it, but VC 7.1 has a whole bunch of backward compatability
    > features that often don't make sense, so maybe thats it.


    You're right. After I posted my comment, I tried an example only to find I was
    incorrect. Thanks,

    /david

    --
    Andre, a simple peasant, had only one thing on his mind as he crept
    along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
    -- unknown
    David Rubin, Feb 20, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,258
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    575
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    588
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    385
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    458
Loading...

Share This Page