gpt4 book ai didi

c++ - 为什么可以在没有对象实例的情况下调用非静态成员函数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:32 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
What will happen when I call a member function on a NULL object pointer?

好吧,我认为这段代码和程序输出可以 self 解释:

#include <iostream>
#include <string>
using namespace std;

class Test
{
public:
void Not_Static(string args)
{
cout << args << endl;
}
};

int main()
{
Test* Not_An_instance = nullptr;
Not_An_instance->Not_Static("Non-static function called with no object?");
cin.ignore();
return 0;
}

程序输出:

Non-static function called with no object?

为什么这是可能的?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com