tags:

views:

103

answers:

4

Hi,

I am writing class with a const reference like this:

class B;

class foo {
  const B& b;

  foo(const B& newb): b(newb) { }

  void operator=(const foo & foo2) {
      // This does not work
      foo.b = foo2.b;
  }   
};

I am trying to define a working operator= Obviously = does not work since I am not allowd to change a const reference. This there a way to let a reference point to another object?

If b is not const c++ will provide me with a working operator=. If I define one member as const cl will spit out:

warning C4512: 'foo' : assignment operator could not be generated

+3  A: 

I don't think your code makes any sense. There is no object called foo, so foo = foo2 is meaningless.

Oli Charlesworth
+3  A: 

No, once a reference is initialized, the object it references cannot be changed. If you need to do that, a pointer is more appropriate.

dauphic
I editet my question and hope that it is now understandable what I am trying to do
plaisthos
+1  A: 

You cannot change which object a reference refers to. Use a pointer in your class instead.

Dave Hinton
+6  A: 

The term you're looking for is "rebind." As in "is it possible to rebind a reference?" The answer is "no, references are defined as not being rebindable." But you can use a pointer and change what it points to:

class B;

class foo {
    B* b;

    foo(const B& newb)
    {
        b = &newb;
    }

    void operator=(const foo& foo2) {
    b = &foo2.b;
  }   

};

However, this risks dangling pointers if the object being assigned to outlives the object used for the assignment (same problem for the constructor, by the way). There's a good chance that you don't need references or pointers at all:

class B;

class foo {
    B b;

    foo(const B& newb): b(newb) { }

    void operator=(const foo& foo2) {
    b = foo2.b;
  }   

};

If you do need pointers/references (maybe B is a base class) then you'll want to look into smart pointers to handle object lifetime issues.

Max Lybbert
B is a *huge* rather large object in this case. And the class should really hold it as reference so if the object changes the changes are visiable too.
plaisthos
A pointer and reference are fundamentally the same thing in this example, except a reference can't be null; the size of the object doesn't matter in either case.
dauphic
@plaisthos: Also, if you assign over a pointer to a B, you might be overwriting the last pointer to that B, and have a memory leak. It sounds like it might not take too many lost Bs to be a serious leak. Seriously consider smart pointers.
David Thornley
@plaisthos: A number of programmers coming from Java assume that pointers or references are the only option. They aren't: plain objects ("value semantics") are usually enough. From what you're saying, you have a good reason to use pointers/references. Since you need rebinding, you need pointers. Since you probably will have to manage the lifetimes of the various objects, you should look at smart pointers, like boost::share_ptr (a.k.a., std::tr1::share_ptr).
Max Lybbert