not so comfortable withreinterpret_castso I'll be very happy if
there were some other solution to this issue.
Is there a way to solve this problem without usingreinterpret_cast?
Your reinterpret_castsolution is non-standard and does not work with
EDG or MINGW (courtesy of Dinkumware.com) or Comeau's online tests,
and the last is usually the most standard compliant compiler around.
I suspected that. Thanks for confirming.
How about duplicating the member in the subclass like this:
// Class Customer
struct Customer : public TableWithId {
const int Userlabel;
const int Id; // Note this line
Customer::Customer()
: TableWithId("Customer", 1)
, Userlabel(2)
, Id(TableWithId::Id) // Note this line
{}
};
Works for me on all the aforementioned compilers and VS2005.
This works but it's not a solution.
In the example I posted, both Customer and SchemaBasedIdToLabelMapper
are in the same source file, while in reality they are two classes
that belongs to two different libraries.
Customer is static from my point of view (i.e. I cannot change it),
while I can operate on the template. So any modification to Customer
is not a solution for me.
Anyway, just for the records, up to now I wasn't able to find any
solution to this problem.
Ciao! --M
Thanks for trying.
Ciao,
Danilo- Nascondi testo citato
- Mostra testo citato -
Hi,
interesting... You can try tagging the base class.
The following compiles.
Hope it helps a little.
Bye,
Francesco
#include <iostream>
using namespace std;
// ABC Table
struct Table {
Table(const string& name);
string name;
};
Table::Table(const string& name) : name(name) {
cout << name << endl;
}
// Class TableWithId (intermediate class)
struct TableWithId : public Table {
/*********************/
typedef TableWithId TableWithIDTag;
/*********************/
const int Id;
TableWithId(const string& name, int id);
};
TableWithId::TableWithId(const string& name, int id) : Table(name),
Id(id) {
}
// Class Customer
struct Customer : public TableWithId {
const int Userlabel;
Customer();
};
Customer::Customer() : TableWithId("Customer", 1), Userlabel(2) {
}
// Class SchemaBasedIdToLabelMapper
template<class C, const int C::TableWithId::* idColumn, const int C::*
labelColumn>
class SchemaBasedIdToLabelMapper {
public:
SchemaBasedIdToLabelMapper();
};
template<class C, const int C::TableWithId::* idColumn, const int C::*
labelColumn>
SchemaBasedIdToLabelMapper<C, idColumn,
labelColumn>::SchemaBasedIdToLabelMapper() {
cout << (void *) this << endl;
C c;
cout << "IdColumn: " << c.*idColumn << endl;
cout << "LabelColumn: " << c.*labelColumn << endl;
}
int main() {
SchemaBasedIdToLabelMapper<Customer, &Customer::Id,
&Customer::Userlabel> p;
}
//END CODE- Nascondi testo citato
- Mostra testo citato -
SOrry, I've posted in a rush. The tagging it's useless and I actually
mistyped it.