ここで、ポインターがぶら下がったときに「なぜこれが機能するのか」という質問がありました。答えは、それが UB であるということでした。つまり、機能するかどうかということです。
チュートリアルで次のことを学びました。
#include <iostream>
struct Foo
{
int member;
void function() { std::cout << "hello";}
};
int main()
{
Foo* fooObj = nullptr;
fooObj->member = 5; // This will cause a read access violation but...
fooObj->function(); // Because this doesn't refer to any memory specific to
// the Foo object, and doesn't touch any of its members
// It will work.
}
これは次と同等ですか:
static void function(Foo* fooObj) // Foo* essentially being the "this" pointer
{
std::cout << "Hello";
// Foo pointer, even though dangling or null, isn't touched. And so should
// run fine.
}
私はこれについて間違っていますか?関数を呼び出すだけで無効な Foo ポインターにアクセスしないと説明したにもかかわらず、それは UB ですか?