🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #9

Re: The difference between X(X&) and X(const X&) ?

#9
From: Warwick Allison
Date: Tue, 27 Oct 1992 05:04
45 lines
1694 bytes
hvaisane@cs.joensuu.fi (H. Vaisanen) writes:

>If X is a class, what is the difference between constructors

>X(X&) and X(const X&) ?

The first may change the parameter's value, the second may not.  I would
expect the first to be the less common usage.

>Example:

>class Foo {
>public:
>  Foo (Foo &f); // This is non-const
>};


>class Bar {
>  Foo _f;
>public:
>  Foo f() const {return _f;}; // Trouble here!
>};

>generates warnings
>a.C: In method `class Foo Bar::f ()const ':
>a.C:11: warning: conversion to reference of read-only member `_f'


This is correct.  The compiler is warning you that in order to create the
return value of f(), it must create a copy of _f using a copy constructor
that does not guarantee to leave the _f unchanged.  Since f() guarantees
that it does not change _f (or any of the rest of the class).

The reason this is a warning rather than an error is for abstract constness
(where the bitpattern of the object does change, but the semantic value
remains unchanged).


--
Warwick
--
  _-_|\      warwick@cs.uq.oz.au            /Disclaimer:
 /     * <-- Computer Science Department,  /  
 \_.-._/     University of Queensland,    /   C references are NULL && void*
      v      Brisbane, Australia.        /  
Technical Details
Message-ID: <10805@uqcspe.cs.uq.oz.au>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!uqcspe!cs.uq.oz.au!warwick
References: <1992Oct23.073051.5352@cs.joensuu.fi>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!uqcspe!cs.uq.oz.au!warwick
From: warwick@cs.uq.oz.au (Warwick Allison)
Newsgroups: gnu.g++.help
Subject: Re: The difference between X(X&) and X(const X&) ?
Message-ID: <10805@uqcspe.cs.uq.oz.au>
Date: 27 Oct 92 05:04:39 GMT
References: <1992Oct23.073051.5352@cs.joensuu.fi>
Sender: news@cs.uq.oz.au
Reply-To: warwick@cs.uq.oz.au
Lines: 45