Consider these lines of code. When I try to compile the compiler will show errors like 'a' is not a member of 'DataType1'. I understand how the compiler treats these as errors but is there any way I could avoid this or another method that works?
struct DataType1 { public: int x; };
struct DataType2 { public: int a; };
template <class E>
bool job2(E* newData, const int i){
int something = 2;
if (i == 1) newData->x = something;
if (i == 2) newData->a = something;
}
template <class E>
bool job1(List<E>* dataBase){
E* newData = new E;
job2(newData, 1);
dataBase->push_back(newData);
}
template <class E>
int main(){
List<DataType1>* dataBase = new List<DataType>;
job1(dataBase);
}
const int ivariable into a template argument and specialize the templated function. But that brings a question: why don't you remove theiargument and just specialize onE?SetI()) in eachDataTypestruct and call that insteadnewData->SetI(something).ias template parameter, you can useif constexprcannot convert argument from E* to DataType1push_backan element of typeE*toList<DataType1>. That's obviously wrong, even whenE=DataType1. The problem isjob1function, notjob2specialization/overload. Not to mention thatjob1leaks memory.