KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I was experimenting with C++ and found the below code as very strange. class Foo{ public: virtual void say_virtual_hi(){ std::cout << "Virtual Hi"; } void say_hi() { std::cout << "Hi"; } }; int main(int argc, char** argv) { Foo* foo = 0; foo->say_hi(); // works well foo->say_virtual_hi(); // will crash the app return 0; } I know that the virtual method call crashes because it requires a vtable lookup and can only work with valid objects. I have the following questions How does the non virtual method say_hi work on a NULL pointer? Where does the object foo get allocated? Any thoughts?
Tags (comma-separated)
Save Edits
Cancel