1

The other day i was trying to create an object by calling the default constructor of another class, and it ended up making a function declaration, Here is an example:

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.
}

Why does this happen? I know and can call the constructor like this integer::integer() but i would like to understand what's happening here and why integer() is behaving like integer(*)() in this example.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Denomycor
  • 157
  • 6

1 Answers1

0

Why does this happen?

The rule comes from C, in C++ it works the same. From c++ standard dcl.fct inside a function parameter list:

... After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”. ...

Inside a function parameter list a parameter that has a type of function type integer() is converted to a pointer to that type integer(*)().

So is integer() in fact equal to integer(*)()?

Inside function parameter list, yes.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111