Error:cannot call member function without object

class Base {
public:
    void virtual f() = 0;//纯虚函数
    void virtuL g() { cout << "Base::g()" << endl; 
};

int main() {
    Base::g();
}

运行以上函数时(只是想测试一下抽象类的函数能否被调用),出现了题目所示的错误,查阅相关资料,发现是因为
没有把成员函数声明为static函数,否则需要通过对象来调用函数,通常成员函数在调用时* this指针*时被当作参数传进。,因此一个const对象不能调用非const函数(此时传入的是const this指针),但是非const对象可以调用const对象,因为会发生隐式转换。

猜你喜欢

转载自blog.csdn.net/unirrrrr/article/details/81291291