#3746. [GESP202412 六级 C++] 第 3 题

[GESP202412 六级 C++] 第 3 题

以下 C++ 代码段中存在语法错误或逻辑错误,( )是正确的。

#include <iostream>
using namespace std;
class MyClass {
public:
    MyClass() {
        cout << "Constructor called!" << endl;
    }
    void display() {
        cout << "Display function called!" << endl;
    }
};
int main() {
    MyClass* obj = NULL;
    obj->display();
    return 0;
}

{{ select(1) }}

  • NULLC++ 中无法用于指针初始化,应使用 nullptr
  • obj 的定义应该是 MyClass obj; 而不是指针类型。
  • obj->display() 语句存在空指针访问错误,obj 应该初始化为一个有效的对象。
  • obj->display() 语句会调用 display() 函数,但它没有输出任何内容。