C++ Primer notes - constexpr, member function const, mutable, class declaration, delegate construction

Table of contents

1. P214 constexpr function

2. P235 Constructor (const)

Three. P245 variable data members

4. Declaration of P250 category

5. P261 delegated constructor


1. P214 constexpr function

A function modified by constexpr can be used in constant expressions, and its return value is an rvalue.

A constexpr function needs to meet the following requirements:

1. The function parameter type and return type must be literal value types.

Literal types: built-in types, reference, pointer types.

2. There can only be one return statement in the function body, and no other statements.

example:

constexpr int func(int i) {
	return 2 * i;
}


int j = 4;
int a[func(2)];//正确
int b[func(j)];//错误

The compiler will check the return value of constexpr to see if it is a constant expression.

For example, the return value of func(2) in the above example is 2*2, which is a constant expression, while the return value of func(j) is 2*j, which is not a constant expression. 

2. P235 Constructor (const)

If you declare a class object as const, you can only get const properties after the constructor has all been executed.

Therefore, constructors cannot be declared const. 

"C++ Primer" says "until the constructor completes the initialization, the const attribute can't really be obtained" I personally think it is not accurate enough. 

Completing the initialization should refer to completing the initialization list. The behavior in the constructor is called assignment, so it may be better to change it to not actually get the const attribute until the constructor is executed. (Criticism welcome)

Three. P245 variable data members

If the member function is const modified, then its member variables cannot be modified, but the member variables modified with the mutable keyword can still be modified.

Mutable is not restricted by const-decorated idiom functions.

class A {
	mutable int i;
    int j;
public:
	void func(int a)const {
		i = a;//正确
        j = a;//错误
	}
};

4. Declaration of P250 category

Members of a class cannot have objects of the same type.

class A {
	A a;//错误
};

This is because members of the same class need to be initialized, but the members are inside their own class, and the class object itself has not yet been initialized, so how can the members of the same class be initialized. 

But members can be pointers or references to themselves , because when using pointers or references, they must be an initialized object.

class A {
	A* a;//正确
};

 The same kind of objects can also be defined in the constructor body , because at this time the initialization list has been completed to complete the initialization.

class A {
public:
	A(){	
        A a;//正确
    }
};

Members can also be static class members. Because static members need to be carried out outside the class, the definition inside the class is just a declaration, and it only belongs to the class itself, not to any class object.

class A {
public:
	static A a;//声明
};
A a;//定义

5. P261 delegated constructor

A delegating constructor can "delegate" other constructors of the same type to perform the initialization process, and the delegating constructor can only be called in the initialization list

class A {
	int i;
	int j;
public:
	A(int a, int b)
	{
		i = a;
		j = b;
	}
	A() 
		:A(1, 2)//委托构造
	{};
};

If we call the default construction to create a class A object, then when calling the default construction, the constructor with parameters will be called first, and then the "control" will be returned to the default construction after execution.

Guess you like

Origin blog.csdn.net/weixin_61857742/article/details/128266624