0

Is the nesting of functions possible in the object oriented languages like C#, Java, C++ etc . If so, can anyone give an example?

Bart Kiers
  • 161,100
  • 35
  • 287
  • 281
sandy101
  • 3,285
  • 3
  • 21
  • 19

4 Answers4

3

Having nested function is irrelevant to being object-oriented.


Community
  • 1
  • 1
kennytm
  • 491,404
  • 99
  • 1,053
  • 989
0

Implementing Nested Functions in C#.

KMån
  • 9,804
  • 2
  • 30
  • 41
0

As other answers have pointed out, because you can create anonymous functions in most languages these days, you can assign such a function object to a variable, and that effectively results in a local function (although recursion may be tricky).

In C++ prior to C++1x anonymous functions (known as lambdas) are not yet available. However, you can still declare a function inside a function, because you can declare a class or struct inside a function. If you make that class implement operator(), then instantiate the class and store it in a named variable, you will have effectively achieved the same as a lamda in C++1x.

The problem is that it will (a) be much more verbose and (b) it won't be permissible as an argument to a template.

Daniel Earwicker
  • 111,938
  • 35
  • 201
  • 280
0

Java is another one OO language. It does not support nested methods, but it does support nested class (it could help in workaround): http://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html

sunny
  • 1,777
  • 3
  • 27
  • 40