This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Copy constructor with non const rhs arg
- From: Eljay Love-Jensen <eljay at adobe dot com>
- To: paul moore <paulmoore100 at hotmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Wed, 12 May 2004 11:26:09 -0500
- Subject: Re: Copy constructor with non const rhs arg
- References: <Sea2-DAV40NjVuMhvqg0000ceab@hotmail.com>
hi Paul,
Hmm, I guess pass-by-value a Copy Constructor doth not make.
Solution #1
--------------------------------
#include <memory>
using namespace std;
class Foo
{
public:
Foo(Foo const& in);
private:
auto_ptr<int> m;
};
Foo::Foo(Foo const& in)
: m(const_cast<Foo&>(in).m)
{
}
Foo FooFactory();
int main()
{
Foo f(FooFactory());
Foo f2 = FooFactory();
}
--------------------------------
Solution #2
--------------------------------
#include <memory>
using namespace std;
class Foo
{
public:
Foo(Foo const& in);
private:
mutable auto_ptr<int> m;
};
Foo::Foo(Foo const& in)
: m(in.m)
{
}
Foo FooFactory();
int main()
{
Foo f(FooFactory());
Foo f2 = FooFactory();
}
--------------------------------
HTH.
--Eljay