tags:

views:

136

answers:

5

I've a class which stores a reference to its parent, the reference is passed in the constructor. If I try to copy an instance I get an error "error C2582: 'operator =' function is unavailable" presumably down to the reference being non-assignable.

Is there a way around this, or do I just change the variable to pointer instead of reference?

e.g (over-simplified but I think has the key points):

class MyClass
{
public:
 MyClass(OtherClass &parent) : parent(parent) {}
private:
 OtherClass &parent;
};

MyClass obj(*this);
.
.
.
obj = MyClass(*this);
+2  A: 

Yes just make the member a pointer. A reference won't be able to be reseated, and there is no work-around.

Edit: @"Steve Jessop" makes a valid point to how work-around the problem using the PIMPL idiom (private implementation using a "d-pointer"). In an assignment, you will delete the old implementation and create a new one copy-constructed from the source object's d-pointer.

Johannes Schaub - litb
"there is no work-around". Just for that, I'm going to mention the possibility of mis-implementing `operator=` to explicitly call the destructor, then construct with placement new and the copy constructor. Or more validly, use pimpl and have `operator=` do a copy-and-swap. The impl itself doesn't need `operator=`, and can still use a reference. Still, it's a lot of boilerplate to write if this is the only feature of pimpl desired...
Steve Jessop
@Steve good point. Adopted.
Johannes Schaub - litb
@Steve Jessop: Sorry, but can you please explain a bit more. I am not sure if I understand the workaround
Chubsdad
@Chubsdad the idea is that your class will be like `class MyClass { class Pimpl; Pimpl *p; public: /* dtor and ctors ... */ void swap(MyClass MyClass(MyClass const MyClass };` and in the C++ file `class MyClass::Pimpl { Parent }; MyClass::MyClass(MyClass const return *this; } void MyClass::swap(MyClass }`. This in some kind could be regarded as a "work around", I suspect :)
Johannes Schaub - litb
Yeah, what he said but with more linebreaks ;-). As I said it's a lot of boilerplate just to avoid a pointer, and on reflection litb's right: if it's a work-around then it's a very long way round.
Steve Jessop
But does not 'new Pimpl(*m.p)' reintroduce the original copying back to us? I think this approach just shifts the problem from MyClass to Pimpl without actually solving it in either place. Not sure if I still get it.
Chubsdad
@Chubsdad nono. The copy constructor is used... You can copy references.
Johannes Schaub - litb
I doubt it litb. This is because a reference is not treated as a first class object (3.9/8). Also it is unspecified if a reference has storage or not. So can references really be copied? I could be wrong though.
Chubsdad
+3  A: 

Yes, if you need to support assignment, making it a pointer instead of a reference is nearly your only choice.

Jerry Coffin
A: 

I would make it a boost::shared_ptr. You can be pretty rough with these and they take care of themselves. Whereas using a raw pointer means tha you have to worry about that object being kept alive

pm100
That's no difference from using a reference... you have to trust the object referred to still exists whether using a reference or raw pointer.
John
its *very* different. boost shared_ptr will keep the object alive as long as somebody points at it, raw pointer or reference will not do that
pm100
A: 

You need to implement a copy constructor and initialize the reference in that copy constructor, to point to the same reference as the original object.

florin
+1  A: 

I don't recommend this at all

but if you are really gung ho about doing this:

#include <new>

MyClass::MyClass(const MyClass &rhs): parent(rhs.parent)
{
}

MyClass &MyClass::operator=(const MyClass &rhs)
{
    if (this!=&rhs)
    {
        this->~MyClass();
        new (this) MyClass(rhs);
    }

    return *this;
}
MSN