[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: c++ question: code duplication across classes
From: Frans Englich <englich () kde ! org>
Date: 2006-09-21 11:19:58
Message-ID: 200609211119.58705.englich () kde ! org
[Download RAW message or body]
On Wednesday 20 September 2006 09:16, Sylvain Joyeux wrote:
[...]
> CRTP or global functions. I really like the CRTP (which stands for
> "Curiously Recurring Template Pattern btw), so I'll go with it. But if
> you're more confident with global functions, use them.
Yes, I would probably go for that one too. It's quite a common pattern to have
a super-class that a sub-class inherits for functionality, but in turn must
supply some data that the super-class needs for computing in its specific
way. The classic OO-way is virtual functions acting as callbacks, but C++ has
this nifty CRTP trick.
I tried having a base class that takes two references to int:
----------------------------------------------
template<int &m_height, int &m_width> class Base
{
public:
// do what you want with m_height/m_width
protected:
/**
* @short This class must be sub-classed. For that
* reason this constructor is protected.
*/
inline Base() {}
};
----------------------------------------------
The idea is to be able to do this:
template<int height, int width>
class Fixed : public Base <height, width>
{
};
The variable-case is even more difficult since the variables aren't in-scope
when Base is instantiated.
I don't get this to work; passing references in templates is tricky[1]. If one
get the simple case to work, one could perhaps store the Base as a member in
the flexible vector(after the int members).
Cheers,
Frans
1.
* Google for " is not a valid template argument for type 'const int&' because
it is not a lvalue"
*
http://www.velocityreviews.com/forums/t290339-template-argument-deduction-on-integer-literals.html
>> 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