首页 > 试题广场 >

下列程序中没有语法错误的语句是() #include st

[不定项选择题]
下列程序中没有语法错误的语句是()
#include <string>
class M {
  public:
        M( string s) {
        n = s;
        num++;
    }
     
      M(M& p) {
        n = p.n;
        num++;
    }
    static int num;
    void show() {
        cout << n << endl;
    }
  private:
        string n;
};
int M::num = 0;
int main() {
     M o1("hello");
     M* p = &o1;
     void(M::*q)() = &M::show;
     
     o1.show(); // 1
     (o1.*q)(); // 2
     p->show(); // 3
     (p->*q)(); // 4
    return 0;
}
  • 1
  • 2
  • 3
  • 4
q 的类型:void (M::*)(),表示一个“指向 M 成员函数、参数空、返回 void”的指针。
用在对象实例上时,需要 “
对象.*成员函数指针” 的语法,再在后面加 () 触发调用。
发表于 今天 18:29:28 回复(0)