Escolar Documentos
Profissional Documentos
Cultura Documentos
Agenda
The Constructor Function Dynamic Allocation of Data Members The Destructor Function The Copy Constructor Function
Although Init() or Set() functions can be used, there is no guarantee that they will be called before using the object.
Object creation
Memory Allocation
Ctor Activation
Example: CPoint_b
Note 1: If the Ctor function does require arguments, the object cannot be created without specifying them. Note 2: Since the Constructor is called when the object is created, arguments should be passed only when allocating the object (and not when defining a pointer to it).
Example: CPoint_both
Just like any other member function, Ctors can be overloaded and have default parameters Beware of ambiguity (can we define default values for all parameters?).
copyrights Elhanan Borenstein
Temp objects
CPoint P1 = {3,4};
Temporary objects can be created by an explicit call to the constructor. CRect R(CPoint(1,2), CPoint(3,4)); The object scope will be only the line it was created in.
There are cases when we wish to modify additional data members when an object is copied. (EXAMPLE?) We can implement a Copy Constructor which will properly copy an object!!!
Note:
or
CPoint P3 = P4;
ByVal Objects
Objects as Arguments
As we recall from the previous lecture, we will always prefer to pass argument objects ByRef rather than ByVal However, if we do pass the argument ByVal, the Copy Constructor will be used.
Return Values
We may still want to return an object as the function return value ByVal. Why? Once again, the Copy Constructor will be used. What will happen if we will try to pass the object to the CC by value?
Questions?