Construct object with itself as reference?

后端 未结 6 1679
小鲜肉
小鲜肉 2021-02-02 03:26

I just realised that this program compiles and runs (gcc version 4.4.5 / Ubuntu):

#include 
using namespace std;

class Test
{
public:
  // copyc         


        
6条回答
  •  我在风中等你
    2021-02-02 04:22

    The reason this "is allowed" is because the rules say an identifiers scope starts immediately after the identifier. In the case

    int i = i;
    

    the RHS i is "after" the LHS i so i is in scope. This is not always bad:

    void *p = (void*)&p; // p contains its own address
    

    because a variable can be addressed without its value being used. In the case of the OP's copy constructor no error can be given easily, since binding a reference to a variable does not require the variable to be initialised: it is equivalent to taking the address of a variable. A legitimate constructor could be:

    struct List { List *next; List(List &n) { next = &n; } };
    

    where you see the argument is merely addressed, its value isn't used. In this case a self-reference could actually make sense: the tail of a list is given by a self-reference. Indeed, if you change the type of "next" to a reference, there's little choice since you can't easily use NULL as you might for a pointer.

    As usual, the question is backwards. The question is not why an initialisation of a variable can refer to itself, the question is why it can't refer forward. [In Felix, this is possible]. In particular, for types as opposed to variables, the lack of ability to forward reference is extremely broken, since it prevents recursive types being defined other than by using incomplete types, which is enough in C, but not in C++ due to the existence of templates.

提交回复
热议问题