C + + within class

Source: Internet
Author: User

1. Concept of the Inner class

If a class is defined inside another class, this inner class is called an inner class. Note at this point that the inner class is a separate class, it is not an external class, and it is not possible to invoke an inner class through an object of an outer class. The external class does not have any privileged access to the inner class.

That is, the inner class is the friend class of the outer class. Note the definition of a friend class in which an inner class can access all members of an external class through an object parameter of an external class. But the outer class is not a friend of the inner class.

2. Internal classes can be defined in the external class of public, protected, private are all possible.

If the inner class definition is public, an object of the inner class can be defined by an external class name:: Inner class name.

If defined in private, the outer object of the inner class cannot be defined, which implements the "implement a class that cannot be inherited" problem.

3. Note that inner classes can directly access static, enumeration members in external classes and do not require an object/class name for an external class.

class a{private:static int k;               int h;public:class b{                     void foo () {                           Cout<<k<<endl;//ok                           //cout<

The cout<

This is the same with the use of friend classes. "If you want to use a member of another class, you must have an object of that class."

class a{private:static int k;               int h;public:class b{                     void foo (a a) {                           Cout<<k<<endl;//ok                           Cout<<a.h<<endl;//ok                     }              };}; int a::k=3;

That's fine.

4. Create an inner class object in the heap:

Class A{public:class b{};}; int _tmain (int argc, _tchar* argv[]) {       a::b*b=new a::b ();       return 0;}

5. The inner class can now be declared in the outer class and then defined outside the outer class:

Class a{private:static int i;public:class B;}; Class A::b{public:void Foo () {Cout<<i<<endl;} //!!! There is no need to add a::i.}; int a::i=3;

This form is more like a friend class. Note here and friend class, do not confuse.

6. sizeof (external Class) = External class, and inner class has no relation.

Class A{public:       class B{int o;};}; int _tmain (int argc, _tchar* argv[]) {       cout<<sizeof (A) <<ENDL;//1       return 0;}


To summarize: In fact, the inner class and friend class are very much like. It's just an internal analogy. The friend class has a bit more permissions: You can access static, enumeration members in an external class without the class name. Others are the same as friend classes.

C + + within class

Related Article

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.