Rebirth: I want to learn C++ on the seventh day (anonymous objects, inner classes)

The main content of this article is the implicit type conversion of the constructor, anonymous objects, and inner classes. I hope to be helpful.

Jump to more high-quality content:

​​​​​​Column: C++ Departure of Rebirth (The average quality score of the article is 93)

Table of contents

Implicit type conversions for constructors

1. Implicit type conversion of single parameter constructor

2. Multi-parameter constructor implicit type conversion

anonymous object

inner class


Implicit type conversions for constructors

1. Implicit type conversion of single parameter constructor

Look at the code below

#include<iostream>
using namespace std;
class A
{
public:
	A(int x)
	{
		_a = x;
	}
private:
	int _a;
};
int main()
{
	A a = 3;
	return 0;
}

 Here this code

A a = 3;

For this, the compilation is completely correct, and the member variable _a of the a object will be initialized to 3. why is that?

Principle: When the above statement is executed, the compiler will automatically check the type on the right side of the equal sign. Since the type does not match (int), 3 will have an implicit type conversion. At this time, the right side of the equal sign is equivalent to A(3), generating a temporary object, and then use this object to copy and construct a.

Of course, this is the principle, and the compiler will optimize: use 3 to directly construct the a object.

2. Multi-parameter constructor implicit type conversion

Look at the following code

#include<iostream>
using namespace std;
class A
{
public:
	A(int x,int y)
	{
		_a = x;
		_b = y;
	}
private:
	int _a;
	int _b;
};
int main()
{
	A a = { 3,4 };
	return 0;
}

Here this code

A a = { 3,4 };

The member variables _a and _b of the a object will be initialized to 3 and 4. Use 3, 4 to directly construct the a object.

Multiple parameters need to be enclosed in { }.

anonymous object

When we only want to call the member method in the class once, we need to create an object specially, and it is too troublesome to use the member method with the object.

#include<iostream>
using namespace std;
class A
{
public:
	void print()
	{
		cout << "hello cpp" << endl;
	}
private:
	int _a;
};
int main()
{
	A a;//专门创建一个对象
	a.print();
	return 0;
}

At this time, anonymous objects make their debut. As the name implies, an anonymous object is an object without a name.

#include<iostream>
using namespace std;
class A
{
public:
	void print()
	{
		cout << "hello cpp" << endl;
	}
private:
	int _a;
};
int main()
{
    A().print();//A()是匿名对象
	return 0;
}

Anonymous object features: constant, with only one line in the life cycle, and the destructor is automatically invoked at the end of the line.

For example:

c730fc466aaa4f5ea5ca555f82c30c20.png

Such a code will report an error because the A() anonymous object is constant, and the left side of the equal sign is a reference of type A, which involves the amplification of permissions.

2a6193984f6345e29cbcbbc62a03bc9a.png

The picture above is the translation of permissions.

inner class

Concept: If a class is defined inside another class, the inner class is called an inner class. The inner class is an independent class, it does not belong to the outer class, let alone access the members of the inner class through the object of the outer class. Outer classes do not have any privileged access to inner classes.

Features: The inner class is the friend class of the outer class. See the definition of the friend class. The inner class can access all members of the outer class through the object of the outer class. But the outer class is not a friend of the inner class (an outer class cannot define an inner class object to access inner class members).

#include<iostream>
using namespace std;
class A
{
public:
	
	class B
	{
	public:
		void funcb()
		{
			A a;//内部类创建对象访问外部类可无视权限(天生是外部类的友元类)
			a._a=2;
		}
	private:
		int _b;
	};
private:
	int _a;
};
int main()
{
    A:: B b;//内部类对象的创建
    b.funcb();
	return 0;
}

That's all for today's sharing. If it is helpful to everyone, remember to bookmark it. I hope that programmers can support the following three times and continue to share knowledge! thanks for reading!

Guess you like

Origin blog.csdn.net/2301_76144863/article/details/132178499