Could operator overloading have worked without references?

前端 未结 5 1900
孤街浪徒
孤街浪徒 2020-12-17 00:07

According to Bjarne Stroustrup, references were introduced into C++ to support operator overloading:

References were introduced primarily to support o

5条回答
  •  有刺的猬
    2020-12-17 00:30

    C++ aims at being strongly typed, so trying to be consistent with that philosophy, it makes sense that an object is-not-a pointer to that object, and I totally agree with sbi about how great it is not to have automatic conversion happening all around (I have in mind multi-contributors projects).

    To address your concern more specifically, people learning C++ can get confused at first by references vs. pointer, but I am not sure it would clarify anything for them to have this kind of automatic conversion happening on their behalf.
    For example :

    Foo ** operator+(Foo **lhs, Foo **rhs)
    {...}
    
    Foo *varFoo1,*varFoo2;
    varFoo1 + &varFoo2;
    

    Following the hypothetical implicit object-to-pointer, should varFoo1 be accepted as an argument to a method expecting Foo ** ? Because the method expects a pointer to Foo * and varFoo1 *is-a* Foo *.

    An other advantage of reference used as arguments :
    const references can receive a rvalue as argument (ex. the classic string literal), while pointers can't.

提交回复
热议问题