简体   繁体   English

内部类中的默认赋值运算符,带有引用成员

[英]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). 我可以通过使用两个单独的C变量解决这个问题,因为它们应该是独立的'C'对象,但这只能隐藏问题(我仍然不明白为什么我不能这样做)。

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? 我是否需要提供自己的赋值运算符和复制构造函数?

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. 在C ++中,你不能(重新)分配引用 - 它们需要在定义时初始化。

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;
}

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. 如果需要这样做,请使用指针而不是引用。

Actually, there's a solution to this. 实际上,有一个解决方案。 You can implement operator= in terms of copy construction , and it will work :) It's a very capable technique for such cases. 您可以在复制构造方面实现operator =,它将起作用:)对于这种情况,它是一种非常强大的技术。 Assuming you do want to support assignment. 假设你确实想支持任务。

C++ doesn't have "inner classes", just nested class declarations. C ++没有“内部类”,只有嵌套的类声明。 "inner classes" are a Java-ism that I don't think are found in other mainstream languages. “内部类”是我认为在其他主流语言中找不到的Java主义。 In Java, inner classes are special because they contain an implicit immutable reference to an object of the containing type. 在Java中,内部类是特殊的,因为它们包含对包含类型的对象的隐式不可变引用。 To achieve the equivalent to C++'s nested declarations in Java requires use of static inner classes; 要在Java中实现等效于C ++的嵌套声明,需要使用静态内部类; static inner classes do not contain a reference to an object of the declaring type. 静态内部类不包含对声明类型的对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM