This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Copy constructor with non const rhs arg


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]