Cem*_*mre 5 c++ inheritance object
我有小代码示例:
#include <iostream>
using namespace std;
class A
{
public:
void print()
{
cout << "Hello" << endl;
}
};
class B: public A
{
public:
B() { cout << "Creating B" << endl;}
};
int main()
{
B b();
b.print(); // error: request for member ‘print’ in ‘b’, which is of non-class type ‘B ()()’
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我改为下面的那个,如果它有效,
B* b = new B();
b->print();
Run Code Online (Sandbox Code Playgroud)
当我在堆栈上分配对象时,为什么它不起作用?
| 归档时间: |
|
| 查看次数: |
559 次 |
| 最近记录: |