Topic: const constructor parameters


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/03/27
Raw View
Harvey Taylor writes:

> Is the const qualifier sufficient to differentiate constructors?

Yup, just as it is for member functions.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: het@portal.ca (Harvey Taylor)
Date: 1997/03/22
Raw View
Greetings,

Is the const qualifier sufficient to differentiate constructors?

For example, in copy constructors, are these two signatures
valid and unique, or are they malformed?

CtorTest(CtorTest & rCtorTest);   // copy ctor
CtorTest(const CtorTest & rCtorTest);   // copy ctor2

Similarly, in assignment operators, are these two signatures
valid and unique, or are they malformed?

CtorTest &operator=(CtorTest & rCtorTest); // assignment operator
CtorTest &operator=(const CtorTest & rCtorTest);// assignment operator2

<cordially>
-het



 "Laugh while you can, monkey boy" -John Whorfin

 Harvey Taylor         Internet: het@portal.ca
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/03/23
Raw View
het@portal.ca (Harvey Taylor) writes:

>... are these two signatures
>valid and unique, or are they malformed?
>
>CtorTest(CtorTest & rCtorTest);   // copy ctor
>CtorTest(const CtorTest & rCtorTest);   // copy ctor2

They are valid and they name distinct constructors.

>Similarly, in assignment operators, are these two signatures
>valid and unique, or are they malformed?
>
>CtorTest &operator=(CtorTest & rCtorTest); // assignment operator
>CtorTest &operator=(const CtorTest & rCtorTest);// assignment operator2

These two signatures are valid and they name distinct assignment operators.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: het@portal.ca (Harvey Taylor)
Date: 1997/03/25
Raw View
In article <5h2dni$6js@mulga.cs.mu.OZ.AU>, fjh@mundook.cs.mu.OZ.AU
(Fergus Henderson) writes:
|
|het@portal.ca (Harvey Taylor) writes:
|
|>... are these two signatures
|>valid and unique, or are they malformed?
|>
|>CtorTest(CtorTest & rCtorTest);   // copy ctor
|>CtorTest(const CtorTest & rCtorTest);   // copy ctor2
|
|They are valid and they name distinct constructors.
|
|>Similarly, in assignment operators, are these two signatures
|>valid and unique, or are they malformed?
|>
|>CtorTest &operator=(CtorTest & rCtorTest); // assignment operator
|>CtorTest &operator=(const CtorTest & rCtorTest);// assignment operator2
|
|These two signatures are valid and they name distinct assignment operators.
|

 Ummm. I guess I should have added a bit more detail.
 The example class below generates strange (to me)
 warnings in MSVC 1.52c and MSVC5.0.

class   Base
{
public:
            Base(int count, char* pname);
    virtual ~Base (void);

private:
// These are defined here to prevent unexpected usage
//
    Base(void);   // default ctor
    Base(Base & rBase);        // copy constructor
    Base(const Base & rBase);  // copy constructor2
    Base &operator=(Base & rBase); // assignment operator
    Base &operator=(const Base & rBase);// assignment operator2
};


 This class will generate warnings C4521 and C4522, respectively,
 for the const versions.

    C4521 : multiple copy constructors specified
 C4522 : multiple assignment operators specified

    Interestingly, the online help for the error shows that
    the compiler thinks it can only have a single copy ctor.
    (There is a similar description of C4522.)

---
Compiler Warning (level 3) C4521
'class' : multiple copy constructors specified
There were multiple copy constructors of a single type specified
for the given class. The first constructor was used.
Make sure that there is only one copy constructor defined for a type.
---

 So the question is:
 Is the MS interpretation of the Standard correct in this case?
 Or why can't I have two versions of the copy ctor and assignment
 operator?

 <cordially>
 -het



 "Laugh while you can, monkey boy" -John Whorfin

 Harvey Taylor         Internet: het@portal.ca
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/03/25
Raw View
het@portal.ca (Harvey Taylor) writes:

> Ummm. I guess I should have added a bit more detail.
> The example class below generates strange (to me)
> warnings in MSVC 1.52c and MSVC5.0.
>
>class   Base
>{
>public:
>            Base(int count, char* pname);
>    virtual ~Base (void);
>
>private:
>// These are defined here to prevent unexpected usage
>//
>    Base(void);   // default ctor
>    Base(Base & rBase);        // copy constructor
>    Base(const Base & rBase);  // copy constructor2
>    Base &operator=(Base & rBase); // assignment operator
>    Base &operator=(const Base & rBase);// assignment operator2
>};

This code is fine.

Note, however, that you don't actually need to declare all those five
to prevent unexpected usage.  Specifying just

    Base(const Base & rBase);  // copy constructor2
    Base &operator=(const Base & rBase);// assignment operator2

or just

    Base(const Base & rBase);  // copy constructor2
    Base &operator=(const Base & rBase);// assignment operator2

is enough.

> This class will generate warnings C4521 and C4522, respectively,
> for the const versions.
>
>    C4521 : multiple copy constructors specified
> C4522 : multiple assignment operators specified
>
>    Interestingly, the online help for the error shows that
>    the compiler thinks it can only have a single copy ctor.
>    (There is a similar description of C4522.)

Section 12.8 [class.copy], paragraphs 2 and 9 of the draft
make it clear that multiple copy constructors and multiple assignment
operators in a single class are allowed.

A compiler is of course allowed to issue whatever bogus warnings it
likes...

>Compiler Warning (level 3) C4521
>'class' : multiple copy constructors specified
>There were multiple copy constructors of a single type specified
>for the given class. The first constructor was used.
>Make sure that there is only one copy constructor defined for a type.

If the MS compiler really does unconditionally use the first constructor,
then it does not conform to the draft.  The correct behaviour is to
resolve the overloading in the normal manner, e.g.

 void foo(Base & target, const Base & const_src, Base & src) {
  target = const_src; // should use const version
  target = src;  // should use non-const version
 }

> So the question is:
> Is the MS interpretation of the Standard correct in this case?

Nope.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]