C + + nested classes and peripheral classes and the access of local classes to member variables "data structure" __ garbled problem

Source: Internet
Author: User

Reference: "Data structure C + + language description"

Concept:

Nested classes are defined in a class, and classes that define nested classes are called peripheral classes. Nested classes can only be used in peripheral classes.

The purpose of defining nested classes is to hide information so that the abstraction of the class can be improved.


Examples are as follows:


Where Class B is a nested class, Class A is a peripheral class, and Class B is defined in Class A's class body.
Some notes:
1. From a scope perspective, nested classes are hidden in the perimeter class, which can only be used in peripheral classes.
2. From the point of view of access rights, the nested class name has the same access rules as the object members of its perimeter class. You cannot access a private member function in an object of a nested class, nor can you establish an object on a nested class in the private part of the outer class.
3, a member function in a nested class can be defined outside of its class body.
4. The members described in the nested class are not members of objects in the perimeter class, and vice versa. A member function of a nested class has no access to the members of the outer class, or vice versa. As a result, nested classes are often treated as not nested class classes when analyzing the member access relationships between nested classes and peripheral classes. The format of the nested class can be written in the following format:



4, this shows that the nested class is just a grammatical nesting.
5. Friends that are described in nested classes do not have access to members of the perimeter class.
6, if the nested class is more complex, you can only in the perimeter class to describe the nested class, the detailed content of the nested can be defined in the outer class in the File field.
7. Nested classes can access static member variables of the perimeter class, even if their access rights are private.



Local class:
A class defined in a function body is called a local class. The local class can only be contacted with objects and functions of its perimeter scope, because the variables in the perimeter scope are independent of the object that is changing the local class. The local class cannot be inherited by the outside. When defining a local class, it is important to note that static member functions cannot be declared in a local class, and that all member functions must be defined in the class body. In practice, local classes are rarely used.


All code:

#include <iostream>
#include <stddef.h>
using namespace std;

A nested class accesses the member variable class
gh_a{

	//const 
	const static int s_data =54

of the perimeter class; Public:
	int task_id;

	Class Gh_b
	{public
		:
			int attribute;
			gh_a* parent;

			Constructor
			Gh_b ()
			{
				attribute =15;
				Parent = (gh_a*) ((char*) This-offsetof (Gh_a,gh_b));
				cout<< "private static variable for peripheral class:" <<s_data<<endl;
			}
	} Gh_b;
};

Local class
void Fun ()
{
	static int s;
	Class A
	{public
	:
		void init (int i)
		{
			s = i;
		}
		void print ()
		{
			cout<< "s=" <<s<<endl;
		}
	};
 A m;
 M.init (ten);
 M.print ();
 cout<< "s=" <<s<<endl;
}

int main ()
{
	gh_a A;
	a.gh_b.parent->task_id =;
	cout<< "Peripheral class member variable values are nested accessed and modified after the value:" <<a.task_id<<endl;
	a.task_id = A.gh_b.attribute;
	cout<< "Nested class Access peripheral class member variable value:" <<a.gh_b.parent->task_id<<endl;
	cout<< "Local class:" <<endl;

	Fun ();
	System ("pause");

return 0;
}

Run Result:










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.