Nested classes and local classes in C + +

Source: Internet
Author: User

C++in theNested classes and local classes

Recently took advantage of the Spring Festival holiday idle, find this C + + Primer learned a few chapters, found that many features of C + + do not know. nested classes and local classes feel pretty useful, simply write their usage.

Nested classes

The so-called nested class is the ability to define another class within a class. The scope of the nested class is only in its upper-level class. Here's an example:

#include <iostream>using namespace Std;class c1{public:    int A;    void foo ();    Class C2    {public    :        int A;        void foo ();    } b;}; void C1::foo () {    a = 1;} void C1::c2::foo () {    a = 2;} int main () {    class C1 F;    F.foo ();    F.b.foo ();    cout << f.a << Endl;    cout << f.b.a << Endl;    return 0;}

In fact , there are similar usages in the C language, in which another struct is nested within a struct, or a union is nested within a struct . We also know thata nested struct or union inC is usually anonymous. It's also possible in C + + , and we can nest another anonymous class within a class. However, the member function of an anonymous class can be defined only in the declaration of the class, because the class has no name and we cannot refer to it externally.

Here is a similar example:

Class C3{public:    int A;    void Foo () {a = 3;}    Class    {public    :        int A;        void Foo () {a = 4;}    } b;}; int main () {    class C3 ff;    Ff.foo ();    Ff.b.foo ();    cout << ff.a << Endl;    cout << ff.b.a << Endl;    return 0;}

Local class

The so-called partial class, which is defined within a function, is a class that can only be used inside this function. Here's an example:

int main () {    class C4    {public    :        int A;        void Foo () {a = 4;}    };    Class C4 ff;    Ff.foo ();    cout << ff.a << Endl;    return 0;}

In general, all member variables of a nested class and a local class are declared to be common. Because these two types are used only in a very small scope, it is not necessary to define interfaces to hide internal information. Therefore, you can change the class to a structso thatyou do not write public .

In addition, the function can not be nested in many words. For example, the following example cannot be compiled.

int main () {    void foo () {cout << "wrong";};    Foo ();}

But there are some workarounds that we can use to simulate a local function. Specifically, by overloading a class's operator () method to simulate a function call, Here's an example:

int main () {    struct    {        void operator () (void)        {            cout << "HELLO" << Endl;        }        int operator () (int a, int b)        {            return a + b;        }    } foo;    Foo ();    cout << foo (1, 2);}



Nested classes and local classes in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.