Maison  >  Questions et réponses  >  le corps du texte

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_lee2714 Il y a quelques jours549

répondre à tous(1)je répondrai

  • 迷茫

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

    Man man();
    被认为是函数声明了啊...

    一个名字叫 man 的函数 返回类型是 Man

    répondre
    0
  • Annulerrépondre