函数指针声明

问题描述 投票:0回答:1

前几天,我试图通过调用另一个类的默认构造函数来创建对象,并且最终进行了函数声明,这是一个示例:

struct integer {
    integer(){} //Default constructor.
};

struct rational {
    rational(integer n, integer d){} //Default constructor.
};

void multiply(rational(), rational()) { //Valid syntax? Takes two function pointers.

}

rational one_half() {
    return rational(integer(), integer()); //Here doesnt make a function declaration.
}

int main() { 

    rational num(integer(), integer()); //Here makes a function declaration,
                                        //instead of constructing a rational object.
    multiply(one_half, one_half); //function taking two function pointers.
}

为什么会这样?我知道并且可以像这样调用构造函数integer::integer(),但是我想了解这里发生了什么,以及为什么在这个示例中integer()的行为像integer(*)()

c++ function function-pointers default-constructor most-vexing-parse
1个回答
0
投票

为什么会这样?

© www.soinside.com 2019 - 2024. All rights reserved.