[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: c++ question: code duplication across classes
From:       Matt Newell <newellm () blur ! com>
Date:       2006-09-20 17:15:25
Message-ID: 200609201015.26129.newellm () blur ! com
[Download RAW message or body]

On Tuesday 19 September 2006 23:16, Benoît Jacob wrote:

A couple of points that I don't think have been answered yet.

1.  Having virtual functions will increase the size of an instance of the 
class by sizeof(void*).

2.  You can do what you want with templates by having the common code not in 
the base class, but in the derived class, and pass the base of the derived 
class as a template argument.

template<class T> class MatrixX
{
...
	unsigned int m_width, m_height;
	inline int width(){ return m_width; }
	inline int height(){ return m_height; }
...
}

template<class T, unsigned int Height, unsigned int Width> class Matrix
{
	inline int width() { return Width; }
	inline int height() { return Height; }
...
}


template<class Base> MatrixShared : public Base
{
	void codeThatDoesGenericStuff() {
		// The function calls are actually inlined(with proper compiler flags)
		int width = width();
		int height = height();
		...
	}
}

to instantiate...

MatrixShared<MatrixX<int>> matrixX;
MatrixShared<Matrix<float,10,10>> matrix;

and you can always derive from those too if you need specific ctors, etc.

template<class T> SpecialMatrix : public MatrixShared<MatrixX<T>>
{
	SpecialtMatrix(...){...}
}

Hope this helps someone.

Matt
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic