在堆栈上分配对象时的C++继承错误

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)

当我在堆栈上分配对象时,为什么它不起作用?

Mar*_*k B 9

由于B b();声明了一个名为函数b返回一个B.只是使用B b;并指责C++有一个复杂的语法,这使得这种结构变得棘手.