Why is this way of declaring a function deprecated in C ++?

This type of function declaration is valid in C, but why not in C++?

int sum(i,j)int i,j;
{
  return i+j;
}
+3
source share
5 answers

This is because defining an old-style function does not declare a prototype. That is, the caller would not know what types of parameters the function expects.

In C ++, this is too big a security issue. In particular, the FDIS says:

Edit: In C ++, the function definition syntax excludes the "old style" function of C. In C, the "old style" syntax is allowed, but it is deprecated as "deprecated."

Rationale: Prototypes are necessary for safety.

: .

: .

: , .

+11

K & R style, C. ANSI:

int sum(int i, int j)

, K & R , . , K & R , , .

+3

, , ++.

Also, this syntax complicates interface declarations. When K and R adopted this style, object-oriented programming based on the interface was not popular.

0
source

Why should it really be? C ++ prefers type safety. You should not use such a function declaration in C: it is very deprecated.

0
source

Indeed, it should be deprecated everywhere, because it is not as clear as:

int sum(int i, int j)
{
    return i+j;
}

... but I assume that it is absent in C ++, especially because it is very similar to (and you can confuse) the initialization performed in the constructor:

void MyClass::MyClass():myVar(5)
{

}
0
source

All Articles