Matrix (C ++ implementation, first version)

xiaoxiao2021-03-06  40

Namespace mylib {

Template

Const size_t line_sz,

Const size_t row_sz>

Class Matrix {

PUBLIC:

// typedef

Typedef Ty value_type;

TYPEDEF SIZE_T SIZE_TYPE;

TYPEDEF PTRDIFF_T DIFFECENCE_TYPE;

TypedEf Ty * Pointer;

Typedef const type * const_pointer;

Typedef Ty & Reference;

Typedef const type & const_reference;

TYPEDEF TY (* CMAT_PTR) [ROW_SZ];

TYPEDEF const type (* const_cmat_ptr) [Row_SZ];

// conntructors

Matrix (void)

: Mat_ptr (NULL)

{

GET_MEMORY ();

For (size_type i = 0; i

For (size_type j = 0; j

MAT_PTR [I] [J] = value_type ();

}

Matrix (const_cmat_ptr cmat)

: Mat_ptr (NULL)

{

GET_MEMORY ();

For (size_type i = 0; i

For (size_type j = 0; j

MAT_PTR [I] [J] = CMAT [I] [J];

}

Matrix (Const Matrix & Mat)

: Mat_ptr (NULL)

{

GET_MEMORY ();

For (size_type i = 0; i

For (size_type j = 0; j

MAT_PTR [I] [J] = MAT [I] [J];

}

// deStructors

~ Matrix (void)

{

DESTROY_MEMORY ();

}

// public Member Methods

Const size_type line_size (void) Const

{

Return line_sz;

}

Const size_type row_size (void) Const

{

Return row_sz;

}

Const size_type size (void) Const

{

RETURN SIZE_TYPE (LINE_SZ * ROW_SZ);

}

// Operator MEMBER

Matrix &

Operator = (const Matrix & mat)

{

IF (this == & mat)

RETURN * THIS;

IF (MAT_PTR! = BULL)

DESTROY_MEMORY ();

GET_MEMORY ();

For (size_type i = 0; i

For (size_type j = 0; j

}

Matrix &

Operator = (const_cmat_ptr cmat)

{

IF (this == cmat)

RETURN * THIS;

IF (MAT_PTR! = BUL L)

DESTROY_MEMORY ();

GET_MEMORY ();

For (size_type i = 0; i

For (size_type j = 0; j

MAT_PTR [I] [J] = CMAT [I] [J];

}

Pointer Operator [] (const size_type limited)

{

IF (LIDX> = line_sz)

{

Std :: CERR << "LIDX> = line_sz" << std :: endl;

Std :: exit (exit_failure);

}

Return Mat_ptr [LIDX];

}

Const_point Operator [] (const size_type limited) const

{

IF (LIDX> = line_sz)

{

Std :: CERR << "LIDX> = line_sz" << std :: endl;

Std :: exit (exit_failure);

}

Return Mat_ptr [LIDX];

}

Operator const_cmat_ptr () Const

{

Return Mat_ptr;

}

Private:

// Private Methods

Void get_memory (void)

{

Mat_ptr = new value_type [line_sz] [row_sz];

}

Void Destroy_Memory (Void)

{

DELETE [] MAT_PTR;

}

// Date Member

CMAT_PTR MAT_PTR;

}

}

转载请注明原文地址:https://www.9cbs.com/read-71251.html

New Post(0)