13.5 Automatically Generated Member Functions

I l @ ve RuBoard

Every class has a constructor and a destructor. If the programmer does not write these member functions, C++ will automatically generate them. Also, there are several member functions, such as the copy constructor, that can be called automatically.

13.5.1 Automatically Generated and Used Functions

The following are the automatically generated and called functions:

class :: class ( )

Default constructor.

Automatically generated if no other constructors are defined. The generated code fills the data members of the class with random values.

Automatically called when a variable of a class is declared with no parameters, such as:

 class_type var; 
class::class(const class& old_class)

Copy constructor.

Automatically generated unless the programmer explicitly defines a copy constructor. The function C++ copies all of the data members from the old class to the new one.

Automatically called when passing a call- by-value parameter to a function. This member function will also be called when creating a duplicate of a variable:

 class_type first_var; // Call copy constructor to // make duplicate of first_var class_type second_var(first_var); 
class ::~ class ( )

Destructor.

Automatically generated unless the programmer defines one.

Automatically called when a variable is destroyed. For automatic variables, this occurs at the end of the block in which the variable is defined. Global and static variables are destroyed when the program exits. (The destructor is also called by the delete operator, discussed in Chapter 20.)

class class::operator = (const class& old_class)

Assignment operator. (Operator overloading is discussed in Chapter 18.)

Automatically generated to handle assignment of one object to another. The function C++ copies all the data members from the old object to the new one:

 class_type var1; class_type var2; var1 = var2;   // "operator =" called 

13.5.2 Explicit Constructors

Suppose you have the following class:

 class int_array {     public:         int_array(unsigned int size); 

We can create an instance of this class using the statement:

 int_array example(10); 

But we can also initialize the array using the statement:

 int_array example = 10; 

Both do the same thing because C++ is smart enough to automatically convert the assignment to a constructor call.

But what if you don't want this conversion to be done? You can tell C++, "Don't play games with the constructor; do exactly what I say!" This is done by declaring the constructor as an explicit construction:

 class int_array {     public:         explicit int_array(unsigned int size); 

Now the we can initialize our variable using the constructor:

 int_array example(10);    // Works with explicit 

But the statement:

 int_array example = 10; // Illegal because of "explicit" 

is now illegal.

It is a good idea to limit the number of side effects and other things that can happen behind your back. For that reason, you should declare your constructors explicit whenever possible.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net