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

List:       kde-devel
Subject:    Re: c++ question: code duplication across classes
From:       Benoit Jacob <jacob () math ! jussieu ! fr>
Date:       2006-09-20 8:26:36
Message-ID: Pine.LNX.4.64.0609201025090.7064 () galois2 ! math ! jussieu ! fr
[Download RAW message or body]

Thanks, good idea... but my fixed size templates (those for which height 
and width are template params) must be size-optimal, so I can't afford to 
add these members.

On Wed, 20 Sep 2006, Sylvain Joyeux wrote:

> - if you went for using virtual methods, you would have better define a
> m_width and m_height attributes in the base class and initialize them
> differently in each derived templates, since accessing an attribute is less
> problematic performance-wise
>
> - you can use non-virtual methods and the CRTP:
>
> 	template<typename Derived>
> 	class MatrixBase
> 	{
> 	public:
> 		void show()
> 		{
> 			int my_height = static_cast<Derived*>(this)->height();
> 		}
> 	};
>
> 	template<int Widht, int Height, typename Type>
> 	class Matrix : MatrixBase< Matrix<Width, Height, Type> >
> 	{
> 		int height() { return Height; }
> 	};
>
> Since no functions are virtual, all calls can be inlined, et voila
> -- 
> Sylvain
>
>>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
>
 
>> 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