C++ Template question
-
I need to crate template class which will be generic parent of more classes containing data. Each child class defines something like item ID (different name in each class, but always int) and I need to let parent class know it's name to be able to make some operations on that. This is pseudocode:
template <class T, ???XXX > class DRecord
{
void DoReset() { ((T*)this) -> XXX = 0; } // accessing the ID
}class A : public DRecord<A, ???A_XXX >
{
int A_XXX;
}XXX is template param I need to define and use some way. I've tried to use member pointers but it didn't compile. Is it possible to define member of child class as parameter into parent class? In moment of parsing the template the member variable is not declared yet, so I'm not sure whether is it possible. Thank you.
-
I need to crate template class which will be generic parent of more classes containing data. Each child class defines something like item ID (different name in each class, but always int) and I need to let parent class know it's name to be able to make some operations on that. This is pseudocode:
template <class T, ???XXX > class DRecord
{
void DoReset() { ((T*)this) -> XXX = 0; } // accessing the ID
}class A : public DRecord<A, ???A_XXX >
{
int A_XXX;
}XXX is template param I need to define and use some way. I've tried to use member pointers but it didn't compile. Is it possible to define member of child class as parameter into parent class? In moment of parsing the template the member variable is not declared yet, so I'm not sure whether is it possible. Thank you.
Why do you want to do that? Could just use polimorphism, couldn't you? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Why do you want to do that? Could just use polimorphism, couldn't you? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I'm creating some database wrapper, it maps tables to classes (or structures). There are some common operations I need to do on primary keys, but each table is using different PK names. I want to code common operations only once (using templates, not preprocessor macros unless necessary) and don't want to use virtual functions (it should be theoretically possible, but I don't know whether C++ templates are powerful enough for this). It is also interesting problem for me since I'm not template expert and I'd like to learn :)
-
I'm creating some database wrapper, it maps tables to classes (or structures). There are some common operations I need to do on primary keys, but each table is using different PK names. I want to code common operations only once (using templates, not preprocessor macros unless necessary) and don't want to use virtual functions (it should be theoretically possible, but I don't know whether C++ templates are powerful enough for this). It is also interesting problem for me since I'm not template expert and I'd like to learn :)
rrrado wrote:
and don't want to use virtual functions
Why don't you (you know base classes practically imply this... :rolleyes: )? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
rrrado wrote:
and don't want to use virtual functions
Why don't you (you know base classes practically imply this... :rolleyes: )? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Because it is not necessary to use virtual tables just to make simple job :) Just for one function <pre>virtual int& GetID();</pre> There are more ways how to do this but I'd like to use clean solution. I have solution with templates for this but it is using extra class and multiple inheritance so I don't like it. This could be useful for me also in future for different purposes so I'd like to know :)
-
I need to crate template class which will be generic parent of more classes containing data. Each child class defines something like item ID (different name in each class, but always int) and I need to let parent class know it's name to be able to make some operations on that. This is pseudocode:
template <class T, ???XXX > class DRecord
{
void DoReset() { ((T*)this) -> XXX = 0; } // accessing the ID
}class A : public DRecord<A, ???A_XXX >
{
int A_XXX;
}XXX is template param I need to define and use some way. I've tried to use member pointers but it didn't compile. Is it possible to define member of child class as parameter into parent class? In moment of parsing the template the member variable is not declared yet, so I'm not sure whether is it possible. Thank you.
How about just presuming the existence of accessor functions in the parent - I've defined a macro to help reduce the amount of typing:
template <class T> class DRecord
{
void DoReset() { static_cast<T*>(this)->SetID(0); } // accessing the ID
};#define DEFINE_ID_FIELD(ID_NAME) \
int NAME;
void SetID(int value) { NAME = value; }
int GetID(int value) const { return NAME; }class A : public DRecord<A>
{
DEFINE_ID_FIELD(A_XXX)
};As an aside - are you aware of the OLE DB consumer templates[^] - they're templated database wrappers...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Because it is not necessary to use virtual tables just to make simple job :) Just for one function <pre>virtual int& GetID();</pre> There are more ways how to do this but I'd like to use clean solution. I have solution with templates for this but it is using extra class and multiple inheritance so I don't like it. This could be useful for me also in future for different purposes so I'd like to know :)
rrrado wrote:
Because it is not necessary to use virtual tables just to make simple job Smile Just for one function
virtual int& GetID();
Since this is your point of view, why do you ask for a 'clean solution'? :rolleyes: :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
How about just presuming the existence of accessor functions in the parent - I've defined a macro to help reduce the amount of typing:
template <class T> class DRecord
{
void DoReset() { static_cast<T*>(this)->SetID(0); } // accessing the ID
};#define DEFINE_ID_FIELD(ID_NAME) \
int NAME;
void SetID(int value) { NAME = value; }
int GetID(int value) const { return NAME; }class A : public DRecord<A>
{
DEFINE_ID_FIELD(A_XXX)
};As an aside - are you aware of the OLE DB consumer templates[^] - they're templated database wrappers...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thank you for idea, I was thinking about it and haven't realized that I don't need virtual accessors for this unless I saw your code. I don't know OLE DB templates, thank you for tip. I'm using sqlite so I guess I cannot use them but maybe there will be some good point in their architecture.
-
Thank you for idea, I was thinking about it and haven't realized that I don't need virtual accessors for this unless I saw your code. I don't know OLE DB templates, thank you for tip. I'm using sqlite so I guess I cannot use them but maybe there will be some good point in their architecture.
rrrado wrote:
I don't know OLE DB templates, thank you for tip. I'm using sqlite so I guess I cannot use them...
I wouldn't bet on it - OLE DB Provider for SQLite[^]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p