Почему не Может я хранить ссылки в 'станд.:: отобразиться' в C++?

Нет никакого недостатка в расширении Number.prototype кроме смущения других людей. Какой смысл? Что лучше в использовании value.round() вместо Math.round(value)?

существует несколько серьезных оснований для эти Math объект:

  1. Это работает на нечисла, также: Math.round("5") работы, тогда как value.round() не будет работать, когда значение является строкой (например, значение текстового поля)
  2. , Некоторые члены Математического объекта не принадлежат "основному" числовому значению, как Math.min() или Math.max(). Или Вы хотите использовать его как a.max(b)?
  3. Другие участники глобальны и не принадлежат специализированному числу. Примерами являются константы как Math.PI или функция Math.random().
41
задан schoetbi 6 March 2019 в 08:14
поделиться

3 ответа

They way I understand it, references are implemented as pointers under the hood. The reason why you can't store them in a map is purely semantic; you have to initialize a reference when it's created and you can't change it afterward anymore. This doesn't mesh with the way a map works.

27
ответ дан 27 November 2019 в 00:46
поделиться

The important difference apart from the syntactic sugar is that references cannot be changed to refer to another object than the one they were initialized with. This is why they cannot be stored in maps or other containers, because containers need to be able to modify the element type they contain.

As an illustration of this:

A anObject, anotherObject;
A *pointerToA=&anObject;
A &referenceToA=anObject;

// We can change pointerToA so that it points to a different object
pointerToA=&anotherObject;

// But it is not possible to change what referenceToA points to.
// The following code might look as if it does this... but in fact,
// it assigns anotherObject to whatever referenceToA is referring to.
referenceToA=anotherObject;
// Has the same effect as
// anObject=anotherObject;
7
ответ дан 27 November 2019 в 00:46
поделиться

You should think of a reference as a 'const pointer to a non-const object':

MyObject& ~~ MyObject * const

Furthermore, a reference can only be built as an alias of something which exists (which is not necessary for a pointer, though advisable apart from NULL). This does not guarantee that the object will stay around (and indeed you might have a core when accessing an object through a reference if it is no more), consider this code:

// Falsifying a reference
MyObject& firstProblem = *((MyObject*)0);
firstProblem.do(); // undefined behavior

// Referencing something that exists no more
MyObject* anObject = new MyObject;
MyObject& secondProblem = *anObject;
delete anObject;
secondProblem.do(); // undefined behavior

Now, there are two requirements for a STL container:

  • T must be default constructible (a reference is not)
  • T must be assignable (you cannot reset a reference, though you can assign to its referee)

So, in STL containers, you have to use proxys or pointers.

Now, using pointers might prove problematic for memory handling, so you may have to:

DO NOT use auto_ptr, there is a problem with assignment since it modifies the right hand operand.

Hope it helps :)

24
ответ дан 27 November 2019 в 00:46
поделиться
Другие вопросы по тегам:

Похожие вопросы: