Review Summary C ++ (c) - Function

Try to use const reference

The function does not change the parameter defined as common references is a common mistake, which will not only bring a misleading function call that function can modify the value of its argument, and also cause less serious error .

int get_size(string& s)
{
	return s.size();
}

This parameter is common function references and therefore unacceptable constant reference, since in C ++, is the literal string const char * type, trying to initialize the reference variable s by the constant const reference would be illegal. For example, the following call illegal.

get_size("abc");  // 非法

main processing command line options

When using the argument in argv, we must remember optional arguments from argv [1] Start; Name argv [0] storing the program, instead of the user input.

// 调用prog -d -o ofile data0
int main(int argc, char* argv)
{
	// argc = 5;
	// argv[0] == "prog";
	// argv[1] == "-d";
	// argv[2] == "-o";
	// argv[3] == "ofile";
	// argv[4] == "data0";
	// argv[5] == 0;
}

Case of a function return value as the value of the left

When the function returns a value type is a reference type, obtained when calls are left value, and the remaining cases are the right values.

int& get_A_i(int* A, int index)
{
	return A[index];
}
int main()
{
	int A[10];
	get_A_i(A, 1) = 1; // 等价于A[1] = 1;
}

Is not required to return a reference to a local variable attention, except modified static local variable, because he has a particular life cycle.

int &get_i()
{
	static int i = 243;
	return i;
}

int main()
{
	get_i() = 1; // 执行完毕后i=1;
}

Declare a pointer to a function returns an array

int(*func(int i))[10];

Can understand the meaning of the declaration under way from the inside out:

  1. Representative declaration func (int i) with an int is a function of the parameter, the name of the function func.
  2. (* Func (int i)) representative of the return value is a pointer type.
  3. (* Func (int i)) [10] is representative of a pointer to an array of length 10.
  4. int (* func (int i)) Type [10] represents the elements in the array is an int.

In C ++, we can use the 11 opposite end of the return type, so that the above statement easier to understand:

auto func(int i) -> int(*)[10];

The return type on the -> pointer after, you can make the statement easier to understand.

Overloading and parameter const

As the top-level const is no requirement at the time of the assignment, for example, it is possible to type int const int type assignment, in turn, is also available. So we have a top-const parameter and can not be one without the top layer of the parameter const distinction:

int func(int i);
int func(const int i);

int func2(int *p);
int func2(int *const p);

And two declarations here func func2 are equivalent. But the underlying const, it can be distinguished, such as:

int func(int &i1);
int func(const int &i2);

int func2(int *p1);
int func2(const int *p2);

At this time, i2 and p2 are const const bottom, because it is not freely switch between the underlying const, such as type const int & i2 not initialize type int & i1, i1 be initialized while i2, but there is a way to select a compiler Match higher function, so this is a legitimate form of overloading.

About default parameters

Local variables can not function as default values, in addition. As long as the type of the expression can be converted to the desired type of molding parameters, which can serve as a default argument expression. E.g:

int wd = 80;
char def = ' ';
int func() { return 0; }

string screen(int a = wd, char b = def, int c = func())
{
	return "";
}

int main()
{
	screen(); //相当于screen(wd,def,func())
	def = '*';
	int wd = 70; // 隐藏外层wd
	screen(); // 虽然wd被隐藏,但是局部变量wd与函数的默认参数没有任何关系,所以该语句等价于screen(80,'*',func());
}

Of particular note is the example of the second screen (), although the definition of a local variable wd wd hidden outside, but the local variables and functions wd default argument has nothing to do, so the default parameter value is still 80 wd .

Debugging Help

C ++ compiler defines __func__ variable whose value is the name of the current debugging function. In addition, the preprocessor defines four useful for debugging name:

  • __FILE__Store the file name string literals.
  • __LINE__Current plastic storage literal line numbers.
  • __TIME__Storing files compile time string literals.
  • __DATE__Storing files compiled date string literal.
int main()
{
	cerr << "Error: " << __FILE__
		<< " : in function " << __func__
		<< " at line " << __LINE__ << endl;
}

The above code output:

Error: c:\users\Prosat\source\repos\solution\solution\main.cpp : in function main at line 217

It can be seen that the compiler hints that these variables we used the code wrong.

Function pointer

The definition, assignment and use the
example, I have a function like this:

bool lengthCompare(const string &s1, const string &s2)
{
	return s1.size() > s2.size();
}

To define a function pointer corresponding need to ensure that the return type and parameter list an exact match (can be converted to each other does not work), for overloaded functions, I must indicate which function to call.

	// pf指向一个函数,返回值是bool,形参是const string &s1, const string &s2。
	bool(*pf)(const string &s1, const string &s2); // 未初始化
    // 赋值
	pf = lengthCompare; // 合法,会自动将lengthCompare的地址赋值给pf。
	pf = &lengthCompare; // 等价于上面的语句
    // 调用
    bool b1 = pf("hello", "goodbye");
	bool b2 = (*pf)("hello", "goodbye"); // (*pf)括号不能少,否则含义不同。

Return pointer to a function

int(*f3(int i))(int *a, int b);

Similarly, we understand the use of the statement from the inside out the way meanings:

  1. Representative f3 (int i) is a function declared with an int parameter, the name of the function f3.
  2. (* Func (int i)) representative of the return value is a pointer type.
  3. int (* func (int i)) (int * a, int b) is representative of a pointer to a parameter of (int * a, int b) function returns the value of int.

The little finger corresponds to the return type declaration:

auto f3(int i) -> int(*)(int *a, int b);

Tail personal feeling really set the return type is easy to understand more, looks very clear.

Released six original articles · won praise 0 · Views 89

Guess you like

Origin blog.csdn.net/qq_33584870/article/details/104693434