[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 12:51:30
Message-ID: 200609211251.30760.englich () kde ! org
[Download RAW message or body]

On Thursday 21 September 2006 11:43, Thiago Macieira wrote:
> Frans Englich wrote:
> >----------------------------------------------
> >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() {}
> >};
> >
> >----------------------------------------------
>
> This will require two *global* variables of type int. You can't pass
> values.
>
> Remember that the instantiated class will have not the value and not the
> pointer address, but the symbol name mangled.

Ok, a new try. Code:

------------------------------------------------------------------------
#include <cstdio> 

template<typename Derived> class Base
{
public:
	int height() const
	{
		return static_cast<const Derived *>(this)->m_height;
	}

	int width() const
	{
		return static_cast<const Derived *>(this)->m_width;
	}
};

template<int Height, int Width> class Fixed : public Base<Fixed<Height, Width> 
>
{
private:
	friend class Base<Fixed<Height, Width> >;
	static const int m_height = Height;
	static const int m_width = Width;
};

class Variable : public Base<Variable>
{
public:
	void setHeight(const int h)
	{
		m_height = h;
	}

	void setWidth(const int w)
	{
		m_width = w;
	}

private:
	friend class Base<Variable>;
	int m_height;
	int m_width;
};

int main(int argc, char *argv[])
{
	Fixed<3, 4> fixed;

	std::printf("Fixed: height: %d\n",	fixed.height());
	std::printf("Fixed: width: %d\n", 	fixed.width());
	std::printf("Fixed: sizeof: %d\n",      sizeof(Fixed<3, 4>));

	Variable variable;
	variable.setHeight(10);
	variable.setWidth(15);

	std::printf("Variable: height: %d\n",	variable.height());
	std::printf("Variable: width: %d\n",	variable.width());
	std::printf("Variable: sizeof: %d\n",   sizeof(Variable));

	std::printf("int sizeof: %d\n",   sizeof(int));

	return 0;
}
------------------------------------------------------------------------

Output:

------------------------------------------------------------------------
Fixed: height: 3
Fixed: width: 4
Fixed: sizeof: 1
Variable: height: 10
Variable: width: 15
Variable: sizeof: 8
int sizeof: 4
------------------------------------------------------------------------

I don't know whether it's very interesting though. It's again a CRTP pattern. 
The only difference is that instead of dispatching to an inline function in 
the Derived class, it directly uses the members. I don't know whether the 
static, const, private int members in Fixed are "evil" in this case.

I guess it can be said that it guarantees compile time resolution(while 
dispatching to a function is a quite different in that matter).


Cheers,

		Frans
 
>> 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