Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Math Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

3.4 Copy Constructor

Consider the following example program:

This code illustrates the use of the copy constructor:

The copy constructor is a very important and special constructor used by the compiler whenever a new object must be created from an old object. Why is it used in the example program? As you might intuitively expect, the result of the addition operation on the right hand side of //1 is an RWMathVec<double>. Hence, the vector d must be constructed from an object of type RWMathVec<double>, requiring the copy constructor.

Note that the presence of the addition operator + on //1 causes the compiler to invoke the global operator function with prototype:

to add b to 1. To use this operator function, the compiler must first interpret T as a double, then convert the integer 1 to a double. According to the prototype, this function returns a result of type RWMathVec<double>, which is fed into the copy constructor.

The net result is that //1 of the example program is completely equivalent to the statement:

which is certainly a lot harder to read!

We will have more to say about how the copy constructor works once we've discussed assignment.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.