This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Copy constructor with non const rhs arg
- From: "paul moore" <paulmoore100 at hotmail dot com>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Wed, 12 May 2004 08:06:14 -0700
- Subject: Copy constructor with non const rhs arg
This code does not compile on gcc (3.2.2 on rh9 - 3.3 on suse 9)
class test
{
public:
int x;
test(test &rhs)
{
x = rhx.x;
}
};
test foo();
int main(int argc, char* argv[])
{
test dd = foo();
return 0;
}
The test dd line gets hit saying there is no match for test::test(test), the
candidates are test::test(test&)
If I change the copy constructor to 'test(const test&)' it compiles fine.
What am I trying to do? In real life the class contains an auto_ptr - since
the auto_ptr copy constructor updates the rhs you cannot pass it in with
const. (Note the auto_ptr copy constructor itself is declared with a non
const rhs - and code using it compiles fine - this is really puzzling)
Also if I change foo to 'test &foo()' it compiles fine (but of course this
is totally different semantics)
Note also that MSFT vs 2003 compiles this code quite happily.