dimanche 1 février 2015

Compile time polymorphism with enums

So, the questions is related to enum inheritance, and i seen a lot of topics here, in which described approaches how to write enum that support inheritance, but it is not the my case, I think.


I found some stuff that I would want to make. So the idea is that I have the class, let it be OpenGL texture class (actually it is) and there is enum that represent all possible values of one property in texture.



class Texture
{
public:
enum Filter
{
LINEAR = 1,
BILINEAR = 2,
ANISOTROPIC = 3
};

public:
Texture(Filter _filter)
: filter(_filter)
{ }

private:
Filter filter;
};


And class that inherits it. Enum in derived class must be redefined, but that I can use further in base class's constructor:



class Texture2d : public Texture
{
public:
enum Filter
{
LINEAR = 1,
BILINEAR = 2
};

public:
Texture2d(Filter _filter)
: Texture(_filter)
{ }
};


but, of course, this doesn't work, because newly created Filter is Texture2d::Filter, but not the Texture::Filter.


I tried to use an enum class like this:



class Texture
{
public:
enum class Filter;

public:
Texture(Filter _filter)
: filter(_filter)
{ }

private:
Filter filter;
};

class Texture2d : public Texture
{
public:
enum Filter
{
LINEAR = 1,
BILINEAR = 2
};

public:
Texture2d(Filter _filter)
: Texture(_filter)
{ }
};


But this is really stupid idea. I think, if I just would want to expand enum I would write some class wrapper, but I need to shrink enum too. So the question is, how to make this thing work on compile time? It is main problem - make this work in compile time. And another one question: actually trying to do such stuff is ok or I must check out architecture of my application?


(All answers will be appreciated)


Aucun commentaire:

Enregistrer un commentaire