abbra: (Default)
[personal profile] abbra
Не хотите больше быть программистом? Оставьте свой след в истории:
http://blog.8thlight.com/dariusz-pasciak/2012/07/03/dereferencing-null-pointer-without-a-seg-fault.html

После того, как прочитали, заходите под кат и ответьте на вопрос: Скажите, а вас учили, что оператор -> над классами в C++ эквивалентен разыменованию указателя в C?

Что интересно, в стандарте С++ в 5.2.5 (Class member access) в третьем абзаце написано:
If E1 has the type “pointer to class X,” then the expression E1->E2 is converted to the equivalent form (*(E1)).E2; the remainder of 5.2.5 will address only the first option (dot).

Правда, 13.5.6 (Class member access) говорит уже более аккуратно об этом:
An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->() exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3)

Date: 2012-07-11 10:16 am (UTC)
From: [identity profile] beldmit.livejournal.com
Я в свое время на похожее поведение уже натыкался.

http://beldmit.livejournal.com/51712.html?nc=6#comments

Оптимизация - страшная вещь.

Date: 2012-07-11 10:33 am (UTC)
From: [identity profile] beldmit.livejournal.com
Вероятно, на практике компиляторы нарушают этот пункт стандарта - потому что шанс, что будет именно описанное поведение, достаточно высок.

Date: 2012-07-11 12:12 pm (UTC)
From: [identity profile] ihar hrachyshka (from livejournal.com)
Если у вас в программе есть хотя бы одно разыменование null-поинтера, вам никто не гарантирует вообще хоть какого-либо поведения (даже запуска функции main). Ибо undefined behavior, а "раз Бога нет, то все можно".

Date: 2012-07-11 12:13 pm (UTC)
From: [identity profile] beldmit.livejournal.com
В моем случае было не разыменование, а вызов метода.

Date: 2012-07-11 12:20 pm (UTC)
From: [identity profile] ihar hrachyshka (from livejournal.com)
[C++11: 5.2.5/2]: For the first option (dot) the first expression shall have complete class type. For the second option (arrow) the first expression shall have pointer to complete class type. The expression E1->E2 is converted to the equivalent form (*(E1)).E2; the remainder of 5.2.5 will address only the first option (dot). In either case, the id-expression shall name a member of the class or of one of its base classes. [ Note: because the name of a class is inserted in its class scope (Clause 9), the name of a class is also considered a nested member of that class. —end note ] [ Note: 3.4.5 describes how names are looked up after the . and -> operators. —end note ]

Date: 2012-10-17 08:42 am (UTC)
From: [identity profile] gineer.livejournal.com
Тоже мне бином Ньютона. :))

Указатель на метод не является частью реализации объекта(структуры) класса.
Только если этот метод виртуальный и\или в нем была бы попытка обращения по this, были бы проблемы.
А так, такому удивлятся может только человек ни разу не видевший свой код в ассемблере\дебагере. :))

April 2016

S M T W T F S
     12
3456789
1011121314 1516
17181920212223
24252627282930

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 30th, 2025 08:34 am
Powered by Dreamwidth Studios