multiple copy constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Glen Able

    multiple copy constructors

    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.


  • David Rubin

    #2
    Re: multiple copy constructors

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

    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

    Comment

    • Guillaume Brocker

      #3
      Re: multiple copy constructors

      David Rubin wrote:
      [color=blue]
      > Glen Able wrote:
      >[color=green]
      >> Any ideas why compilers seem to give warnings about multiple copy
      >> constructors e.g.
      >>
      >> class A
      >> {
      >> public:
      >> A(A& a);
      >> A(const A& a);
      >> };[/color]
      >
      > The problem is that a two functions cannot be differentiated solely on
      > the basis of the 'constness' of their arguments.[/color]

      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

      Comment

      • John Harrison

        #4
        Re: multiple copy constructors

        >[color=blue]
        > 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)
        >[/color]

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

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

        john


        Comment

        • Rob Williscroft

          #5
          Re: multiple copy constructors

          David Rubin wrote in news:c12p1g$fa5 @netnews.proxy. lucent.com:
          [color=blue]
          > Glen Able wrote:
          >[color=green]
          >> Any ideas why compilers seem to give warnings about multiple copy
          >> constructors e.g.
          >>
          >> class A
          >> {
          >> public:
          >> A(A& a);
          >> A(const A& a);
          >> };[/color]
          >
          > The problem is that a two functions cannot be differentiated solely on
          > the basis of the 'constness' of their arguments.[/color]

          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.
          --

          Comment

          • David Rubin

            #6
            Re: multiple copy constructors

            Rob Williscroft wrote:
            [color=blue]
            > David Rubin wrote in news:c12p1g$fa5 @netnews.proxy. lucent.com:
            >
            >[color=green]
            >>Glen Able wrote:
            >>
            >>[color=darkred]
            >>>Any ideas why compilers seem to give warnings about multiple copy
            >>>constructo rs e.g.
            >>>
            >>>class A
            >>>{
            >>>public:
            >>> A(A& a);
            >>> A(const A& a);
            >>>};[/color]
            >>
            >>The problem is that a two functions cannot be differentiated solely on
            >>the basis of the 'constness' of their arguments.[/color]
            >
            >
            > No, all the usual overload resolution rules apply:[/color]

            [snip][color=blue]
            > 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.[/color]

            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

            Comment

            Working...