Saturday, October 22, 2011

Nested Class or Function Definition?

(Short Notes)

Class composition is of course allowed for object-oriented programming languages. Also, the nested definition for classes is allowed in C++, Java and Python (from my own experience, and other languages may also support this).

But for nested function definition, there are some differences. For C++ and Java, neither allows nested function definition. However, Python does allow nested definition for functions, but such nested functions are only available within the outer function body, unlike the nested class definition which can be accessed from other scopes if defined properly (like defined as public).

Of course, nested function calls are always allowed.

Update:
Function defined in class?
==> member functions

Class defined in function?
==> local classes (link)

In this sense, it's allowed to define member functions of a local class in an enclosing function.

References:
IBM publib

Related Posts:
Class Templates in C++

No comments:

Post a Comment