Const int passed to a templated method

Benjamin Kosnik bkoz@redhat.com
Thu Sep 28 13:54:00 GMT 2006


> class Display
> {
> public:
>     static const int TYPE1;
>     static const int TYPE2;
> 
>     Display(const int& t) : _t(t) {}
>     virtual void process() {}
> private:
>     const int _t;
> };
> const int Display::TYPE1 = 1;
> const int Display::TYPE2 = 2;
> 
> class DisplayType1 : public Display {
> public:
>     DisplayType1() : Display(Display::TYPE1) {}
>     void process() { std::cout << "Type1\n"; }
> };
> 
> class DisplayType2 : public Display {
> public:
>     DisplayType2() : Display(Display::TYPE2) {}
>     void process() { std::cout << "Type2\n"; }
> };

This is not the correct list for this type of question. Instead, I
suggest you read "Modern C++ Design" by Andrei Alexandrescu. He also
has a useful website: I suggest you look at it.

That said:

class display_policy_type1 
{ 
  void process() { std::cout << "Type1\n"; }
};

class display_policy_type2 
{ 
  void process() { std::cout << "Type2\n"; 
};

template<typename Policy>
class display : public Policy { };

is the usual design. 

-benjamin



More information about the Libstdc++ mailing list