Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default assignment operator in inner class with reference members

I've run into an issue I don't understand and I was hoping someone here might provide some insight. The simplified code is as follows (original code was a custom queue/queue-iterator implementation):

class B
{
public:
    B() {};
    class C
    {
    public:
        int get();
        C(B&b) : b(b){};
    private:
        B& b;
    };
public:
    C get_c() { return C(*this); }
};

int main()
{
    B b;
    B::C c = b.get_c();


    c = b.get_c();
    return EXIT_SUCCESS;
}

This, when compiled, gives me the following error:

foo.cpp: In member function 'B::C& B::C::operator=(const B::C&)':
foo.cpp:46: error: non-static reference member 'B& B::C::b', can't use default assignment operator
foo.cpp: In function 'int main()':
foo.cpp:63: note: synthesized method 'B::C& B::C::operator=(const B::C&)' first required here

I can go around this by using two separate C variables, as they are supposed to be independent 'C' objects, but this only hides the problem (I still don't understand why I can't do this).

I think the reason is that the reference cannot be copied, but I don't understand why. Do I need to provide my own assignment operator and copy constructor?

like image 721
laura Avatar asked Dec 02 '09 12:12

laura


People also ask

What is the default assignment operator?

If a class definition does not declare a parameterless constructor, a copy constructor, a copy assignment operator, or a destructor, the compiler will implicitly declare them. These are called default operators.

Is assignment operator present by default in a class?

CPP. The compiler doesn't create default assignment operator in the following cases: 1. Class has a non-static data member of a const type or a reference type.

What is the default assignment operator C++?

In the C++ programming language, the assignment operator, = , is the operator used for assignment. Like most other operators in C++, it can be overloaded.

Is assignment operator overloaded by default in C++?

An Assignment/Copy Assignment( = ) operator is provided by any C++ compiler implicitly unless you have const or reference members in your class.


2 Answers

This problem has nothing to do with inner classes. In C++ you just can't (re)assign references - they need to be initialised when defined.

A simpler example is:

class B
{
public:
    B(int& i) : ir(i) {};

    int& ir;
};


int main()
{
    int i;
    B b(i);      // Constructor - OK

    int j;
    B bb = B(j); // Copy constructor - OK

    bb = b;      // Assignment - Error
    return 0;
}
like image 92
Seb Rose Avatar answered Sep 23 '22 16:09

Seb Rose


A reference cannot be changed after being given its initial value. This means that it is impossible to write an assignment operator that changes the value of a reference member. If you need to do this, use a pointer instead of a reference.

like image 30
interjay Avatar answered Sep 20 '22 16:09

interjay