How to prevent overload?

Is it possible to prevent overloading user-defined functions in C ++? Suppose I have a function:

void foo(int , int);

Can I prevent overloading foo, and if so, how? If possible, can this be expanded to prevent overriding methods through inheritance?

+3
source share
6 answers

In a word: no. You cannot prohibit overloading foowith another signature somewhere else, and you cannot prevent overriding virtual functions.

In the C ++ world, you need to trust people writing code that ends in your program.

+10
source

Section 13.1.1 through 13.1.3 of the standard describes the types of functions that cannot be overloaded:

:

  • , , .
  • - , (9.4).

: 8.3.5, , , :

  • , typedef, . , (7.1.3).
  • , * [], . , (8.3.5). (8.3.4).
  • , , , - , . , (8.3.5).
  • , / , . , - , , .
  • , , .

, .

+5

.

+3

?

+2

, UncleBens, , , , . , . ( , ).

, , . . .

-1

- C:

extern "C" void foo(int , int);

This works because with C-binding, the names are not distorted, so you cannot overload (which uses the encoding of argument types in the character name).

Obviously, this will not apply to member functions.

-2
source

Source: https://habr.com/ru/post/1717763/


All Articles