I l@ve RuBoard |
![]() ![]() |
13.5 Automatically Generated Member FunctionsEvery 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 FunctionsThe following are the automatically generated and called functions:
13.5.2 Explicit ConstructorsSuppose 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 |
![]() ![]() |