search

Home  >  Q&A  >  body text

c++ - 实例化类时 加不加括号问题

实例化类时 会出现两种情况 如下 :

  1. 有括号 无法使用 最后一个注释内的语句

  2. 无括号 则能用

error: request for member 'Person:: func' in 'man', which is of non-class type 'Man()'

#include <iostream>

using namespace std;

class Person
{
public:
    void func()
    {
        cout << "base func" << endl;
    }
};

class Man: public Person
{
public:
    void func()
    {
        cout << "deperive func" << endl;
    }
};

int main()
{
    Man man();

    cout << "man.Person::func()显示调用的输出是:";
    
    /**********************************************/
    //man.Person::func();   
    /*********************************************/
    
    system ( "pause" );
}

有无括号的区别是什么 网上我并没有找到我认为合理的答案

ringa_leeringa_lee2767 days ago600

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-17 15:06:11

    Man man();
    is considered a function declaration...

    A function named man has a return type of Man

    reply
    0
  • Cancelreply