c++ method that disables value calls of functions

  • 2020-11-18 06:23:05
  • OfStack

Code compilation environment: VS2017+Debug+Win32

Depending on the form of the argument, C++ should have three types of function calls: value calls, reference calls, and pointer calls. When parameters are passed as arguments to variables of basic data types, the efficiency of pass-through calls is not much different from that of reference calls and pointer calls. However, for class types, there is a big difference between a value call and a reference call, and the bigger the size of the class object, the bigger the difference.

The difference between a pass-through call and the latter two is that a pass-through call creates a copy of an argument on the stack before entering the body of the function, whereas a reference and pointer call does not. Creating a copy is done using a copy constructor. Therefore, to prohibit value passing calls, you must work on the copy constructor of the class.

You can throw an exception directly in the copy constructor, which forces the programmer not to use the copy constructor, otherwise the program will always run with runtime errors. This is not a good idea, however, and the programmer should be told at compile time that a copy constructor for the class is not available.

1. Is it ok not to show the definition copy constructor?


#include <iostream>
using namespace std;

class A
{
public:
	int num;
	A(){num=5;}
};

void show(A a)
{
	cout<<a.num<<endl;
}

int main()
{
	A obj;
	show(obj);
}

The above program smoothly through the compilation, and output 5. Therefore, not showing the defined copy constructor does not prevent calls to the class's copy constructor, because the compiler automatically provides a default copy constructor for classes that do not display the defined copy constructor.

2. Display define copy constructor and set access to private

The above program adds the definition of the copy constructor and modifies it as follows.


#include <iostream>
using namespace std;

class A
{
	A(const A&){};
public:
	int num;
	A(){num=5;}

};

void show(A a)
{
	cout<<a.num<<endl;
}

int main()
{
	A obj;
	show(obj);
}

This program did not compile in VS2017 and got the following error: error C2248: "A::A" : the private member could not be accessed (declared in the "A" class).
This prevents the object of class A from making a function call in the form of value passing. To get the program to compile, you need to change the definition of the show() function to look like this:


void show(const A& a)
{
	cout<<a.num<<endl;
}

3. Copy constructor instructions

(1) If the reference symbol in the copy constructor is removed & The compile will not pass with the following error message: Illegal copy constructor: the first argument should not be "A". The reason is that if the argument in the copy constructor is not a reference, in the form of A(const A a), then it is equivalent to passing a value (ES45en-ES46en-ES47en), which calls the copy constructor of the class, resulting in an infinite recursive call to the copy constructor. So the argument to the copy constructor must be either a reference or a pointer.

(2) The arguments to the copy constructor are usually const, but const is not strictly required.

(3) As a side note, the copy constructor is called in the following cases:

a. Explicitly or implicitly initializes another object of the same type with 1 object;
b. Passed to a function as an argument by value;
c. When an object is returned, the copy constructor of the return value type is also called.
d. When you need to generate a temporary class object (the class object is created as a function return value).

The above is the c++ function to prohibit the value of the call method details, more information about c++ function to prohibit the value of the call please pay attention to other related articles on this site!


Related articles: