C + + Internal classes and external classes (Java)

Source: Internet
Author: User

(1) Introduction:

An inner class is actually a type of local data that is defined within a class declaration. (very similar to the struct node declaration), which differs from Java.

----internal class declarations have public and private points

If declared as public, it can also be used to define variables, such as Outer::inner var

If the declaration is private, then the outside can not be used to define variables, then Outer::inner var will cause compilation errors. (2)----internal class declaration can be used to define variables

this is the same as defining variables for other data types, as are access rules. Without him ----mutual access to internal and external classes

cannot be accessed, it is entirely dependent on the defined properties of the member variable. (3) code implementation (see note below, not too much explanation here)

#include <iostream> using namespace std;
             Class A {Public:class B1 {public:int A;
             Private:int b;      public:void foo (A &p) {cout << p.i1 << Endl;      OK, because I1 is public in class A cout << p.i2 << Endl;  
	Fail, because I2 is private in class A}};
            Private:class B2 {Public:int A;
            Private:int b;      public:void foo (A &p) {cout << p.i1 << Endl;      OK, because I1 is public in class A cout << p.i2 << Endl;
  
    Fail, because I2 is private in class A}};
		PUBLIC:B1 B11;
		B2 B12;
	int i1;
		PRIVATE:B1 B21;
		B2 B22;
	int i2;      Public:void f (b1& p) {cout << p.a << Endl;      OK, because a is public in class B1 cout << p.b << Endl; Fail, because B is private IN class B1} void F (b2& p) {cout << p.a << Endl;      OK, because a is public in class B2 cout << p.b << Endl;
Fail, because B is private in class B2}};
	int main (int argc, char *argv[]) {a A;                              A::B1 AB1;
	OK, because B1 is declared as public inner class.                              A::B2 ab2;
Fail, because B2 is declared as private inner class return 0; }


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.