'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'Chapter 19. Complex classes need the COPY CONSTRUCTOR and an overload of the = operator
'Note the tilde "~" for the DESTRUCTOR
'If you cast an object like a bcx_vector, everything will happen correctly.
'For example, if bcx_vector *k* contains *(4, 7)*, after the cast *m = k* the
'bcx_vector *m* will contain *(4, 7)* too. The values of k.x and k.y have simply
'been copied to m.x and m.y.
'Now suppose you're playing with objects like the person class below. Those objects
'contain a pointer to a character string. If you cast the person object by writing
'*p = r* it is necesary that some function does the work to make *p* be a correct
'copy of *r*. Otherwise, p.name will point to the same physical character string as
'r.name. What's more, the former character string pointed to by p.name is lost and
'becomes a memory zombie. The result will be catastrophic: a mess of pointers and
'lost data. The methods that will do the job are the COPY CONSTRUCTOR and an overload
' of the = operator
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
$CPPHDR
$NOMAIN
'Translate with MinGW Win no Win Includes OR
'Translate with MinGW Win no Win Inc Unicode
'For Unicode uncomment the next line and add -municode to compile line
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER.TXT"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "NUWENGPP.BAT $FILE$ -m64 con"
'Translate with Win no Win Includes OR
'Translate with Win no Win Inc Unicode
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'==============================================================================
Class person
public:
Raw As char Ptr name
Raw As int age
Constructor person (n = "no name" AS const char Ptr, a = 0 As int)
name = new char[100]
name$ = n$
age = a
End Constructor
Constructor person (s As const person &) ' The COPY CONSTRUCTOR
name = new char[100]
name$ = s.name$
age = s.age
End Constructor
Function operator= (s As const person &) As person& ' overload of =
name$ = s.name$
age = s.age
Function = *this
End Function
Destructor ~person ()
delete [] name
End Destructor
End Class
'==============================================================================
Sub modify_person (h As person&)
h.age += 7
End Sub
'==============================================================================
Function compute_person (h As person) As person
h.age += 7
Function = h
End Function
'==============================================================================
Function main () As int
Raw As person p
cout << p.name << ", age " << p.age << endl << endl
' output: no name, age 0
Raw As person k ("John", 56)
cout << k.name << ", age " << k.age << endl << endl
' output: John, age 56
p = k
cout << p.name << ", age " << p.age << endl << endl
' output: John, age 56
p = person ("Bob", 10)
cout << p.name << ", age " << p.age << endl << endl
' output: Bob, age 10
' Neither the copy constructor nor the overload
' of = are needed for this operation that modifies
' p since just the reference towards p is passed to
' the function modify_person:
modify_person (p)
cout << p.name << ", age " << p.age << endl << endl
' output: Bob, age 17
' The copy constructor is called to pass a complete
' copy of p to the function compute_person. The
' function uses that copy to make its computations
' then a copy of that modified copy is made to
' return the result. Finaly the overload of = is
' called to paste that second copy inside k:
k = compute_person (p)
cout << p.name << ", age " << p.age << endl << endl
' output: Bob, age 17
cout << k.name << ", age " << k.age << endl << endl
' output: Bob, age 24
Pause
Function = EXIT_SUCCESS
End Function
Result:
no name, age 0
John, age 56
John, age 56
Bob, age 10
Bob, age 17
Bob, age 17
Bob, age 24