Thread: constructors and qualifiers

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    46

    constructors and qualifiers

    Hi,

    After years of C programming, I'm starting to learn C++ and am amazed at the number of keywords and how much more complex the syntax is. It is becomming clear to me that C++ is meant for big programs.

    I'm writing a template matrix class as an exercise. I am modeling it on the STL vector template class. I am wondering about my use of the keywords inline, const, delete vs delete[], explicit.

    In the following code have I used the words inline and const in the correct places? Should I be using delete or delete[] in my destructor? Should I be using the word explicit in any of my constructors or anywhere else?

    Thanks. Happy New Year!

    Peter

    Code:
    #include <iostream>
    
    typedef unsigned long size_type;
    
    template <class Type>
    class Matrix {
    public:
    	Matrix(size_type ro=0, size_type co=0);
    	Matrix(size_type ro, size_type co, Type val);
    	Matrix(const Matrix &ma);
    	~Matrix() {delete mat;}
    	
    	size_type rows() const {return r;}
    	size_type cols() const {return c;}
    	void display() const;
    	
    protected:
    	void init(size_type ro, size_type co);
    	
    	size_type r, c;
    	Type *mat;
    };
    
    template <class Type>
    inline void Matrix<Type>::init(size_type ro, size_type co){
    	mat = new Type[ro*co];
    	assert (mat != 0);
    	r = ro;
    	c = co;
    }
    
    template <class Type>
    inline Matrix<Type>::Matrix(size_type ro, size_type co){
    	init(ro,co);
    }
    
    template <class Type>
    inline Matrix<Type>::Matrix(size_type ro, size_type co, Type val){
    	init(ro,co);
    	
    	unsigned int i, j;
    	for (i=0; i<r; i++){
    		for (j=0; j<c; j++){
    			mat[i*c+j] = val;
    		}
    	}
    }
    
    template <class Type>
    inline Matrix<Type>::Matrix(const Matrix &ma){
    	init(ma.r,ma.c);
    	
    	unsigned int i, j;
    	for (i=0; i<r; i++){
    		for (j=0; j<c; j++){
    			mat[i*c+j] = ma.mat[i*c+j];
    		}
    	}
    	
    }
    
    template <class Type>
    void Matrix<Type>::display() const{
    	unsigned int i, j;
    	
    	std::cout << "[" << std::endl;
    	for (i=0; i<r; i++){
    		std::cout << "[";
    		for (j=0; j<c; j++){
    			std::cout << mat[i*c+j];
    			if (j < c-1) std::cout << " ";
    		}
    		std::cout << "]" << std::endl;
    	}
    	std::cout << "]" << std::endl;
    }
    
    int main (void) {
    
    	Matrix<double> mat(2,4,-9.9);
    	
    	std::cout << "mat = ";
    	mat.display();
    	std::cout << "(rows, cols) = (" << mat.rows() << ", "
    	          << mat.cols() << ")" <<  std::endl;
    		
    				
    	Matrix<double> mat2;
    	std::cout << "mat2 = ";
    	mat2.display();	
    	std::cout << "(rows, cols) = (" << mat2.rows() << ", "
    	          << mat2.cols() << ")" <<  std::endl;
    	
    	const Matrix<double> mat3 = mat;
    	std::cout << "mat3 = ";
    	mat3.display();	
    	std::cout << "(rows, cols) = (" << mat3.rows() << ", "
    	          << mat3.cols() << ")" <<  std::endl;
    	
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    In practice, inline function is good for small, frequently used functions. It is essentially a macro with type-check.

    Based on the explanation above, come with up a sound decision for your design.

    Kuphryn

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If your new statement has brackets, so should your delete statement. Conversely, if your new statement has no brackets, your delete statement should have no brackets. It's that simple.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    I was more interested in the placement of the keyword inline.

    I will switch my destructor to be

    Code:
    ~Matrix() {delete[] mat;}
    I thought that maybe delete was used with new and that delete[] was used only with new[]

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by petermichaux
    I thought that maybe delete was used with new and that delete[] was used only with new[]
    That's exactly what I said.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    I was confused because I saw some text that had the operator new[] in a discussion about overloading. I thought this would appear in a statement like
    Code:
    int *ip = new[] int[100];
    .

    Now I realize that the operator new[] gets split up with a type name when used like
    Code:
    int *ip = new int[100];
    .

    (This C++ syntax is all new to me...and seems strange right now.)

    Thanks for you help.

    Peter

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Oh, okay. Now I see where you were confused. Sorry I snapped.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed