13.2 Constructors

Team-Fly

13.2 Constructors

Constructors are functions for the generation of objects of a particular class. For the LINT class this can occur with or without initialization, where in the latter case an object is created and the required memory for the storage of the number is allocated, but no value is assigned to the object. The constructor required for this takes no argument and thus takes on the role of the default constructor of the class LINT (cf. [Str1], Section 10.4.2). The following default constructor LINT(void) in flintpp.cpp creates a LINT object without assigning it a value:

 LINT::LINT (void)   {     n_l = new CLINT;     if (NULL == n_l)       {          panic (E_LINT_NHP, "constructor 1", 0, __LINE__);       }     maxlen = CLINTMAXDIGIT;     init = 0;     status = E_LINT_OK;   } 

If a newly generated object is also to be initialized with a numerical value, then a suitable constructor must be invoked to generate a LINT object and then assign to it a predefined argument as the value. Depending on the type of argument various overloaded constructors must be provided. The class LINT contains the constructor functions as shown in Table 13.1.

Table 13.1: LINT constructors

Constructor

Semantics: generation of a LINT object

LINT (void);

without initialization

(default constructor)

LINT (const char* const, const unsigned char);

from a character string, with the basis of the numerical representation given in the second argument

LINT (const UCHAR* const, const int)

from a byte vector with the length given in the second argument

LINT (const char* const);

from a character string, optionally with prefix 0X for hex numbers or 0B for binary digits

LINT (const LINT&);

from another LINT object

(copy constructor)

LINT (const int);

from a value of type char, short, or integer

LINT (const long int);

from a value of type long integer

LINT (const UCHAR);

from a value of type UCHAR

LINT (const USHORT);

from a value of type USHORT

LINT (const unsigned int);

from a value of type unsigned integer

LINT (const ULONG);

from a value of type ULONG

LINT (const CLINT);

from a CLINT object

We would now like to consider a further example for the LINT construction of the function LINT (const char* const), which generates a LINT object and associates to it a value taken from a character string with ASCII digits. A prefix can be given to the digits contained in the string that contains information about the base of the numerical representation. If a character string is prefixed with 0x or 0X, then hexadecimal digits from the domains {0,1,...,9} and {a,b,...,f}, respectively {A,B,...,F}, are expected. If the prefix is 0b or 0B, then binary digits from the set { 0,1 } are expected. If there is no prefix at all, then the digits are interpreted as decimal digits. The constructor employs the function str2clint_l() to transform the character string into an object of type CLINT, from which then in the second step a LINT object is created:

 LINT:: LINT (const char* const str)   n_l = new CLINT;   if (NULL == n_l) // error with new?     {       panic (E_LINT _NHP, "constructor 4", 0, __LINE__);     }   if (strncmp (str, "0x", 2) == 0 || strncmp (str, "0X", 2) == 0)     { 

       int error = str2clint_l (n_l, (char*)str+2, 16);     }   else     {       if (strncmp (str, "0b", 2) == 0 || strncmp (str, "0B", 2) == 0)          {            error = str2clint_l (n_l, (char*)str+2, 2);          }       else          {            error = str2clint_l (n_l, (char*)str, 10);          }     }   switch (error) // evaluation error code     {       case 0:          maxlen = CLINTMAXDIGIT;          init = 1;          status = E_LINT_OK;          break;       case E_CLINT_BOR:          panic (E_LINT_BOR, "constructor 4", 1, __LINE__);          break;       case E_CLINT_OFL:          panic (E_LINT_OFL, "constructor 4", 1, __LINE__);          break;       case E_CLINT_NPT:          panic (E_LINT_NPT, "constructor 4", 1, __LINE__);          break;       default:          panic (E_LINT_ERR, "constructor 4", error, __LINE__);     } } 

Constructors make possible the initialization of LINT objects among themselves as well as LINT objects with standard types, constants, and character strings, as the following examples demonstrate:

   LINT a;   LINT one (1);   int i = 2147483647;   LINT b (i);   LINT c (one);   LINT d ("0x123456789abcdef0"); 

The constructor functions are called explicitly to generate objects of type LINT from the specified arguments. The LINT constructor, which, for example, changes unsigned long values into LINT objects, is embodied in the following function:

 LINT::LINT (const USHORT ul) {   n_l = new CLINT;   if (NULL == n_l)     {       panic (E_LINT_NHP, "constructor 11", 0, __LINE__);     }   ul2clint_l (n_l, ul);   maxlen = CLINTMAXDIGIT;   init = 1;   status = E_LINT_OK; } 

Now we must provide a destructor function to go with the constructors of the class LINT, which enable the release of objects and, in particular, the memory bound to them. To be sure, the compiler would gladly make a default destructor available to us, but this would release only the memory that the elements of a LINT object possess. The additional memory allocated by the constructors would not be released, and memory leakage would result. The following short destructor fulfills the important tasks of releasing memory occupied by LINT objects:

 ~LINT()  {    delete [] n_l;  } 


Team-Fly


Cryptography in C and C++
Cryptography in C and C++
ISBN: 189311595X
EAN: 2147483647
Year: 2001
Pages: 127

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