I believe the code will help simplify my question.
I would like to be able to write:
template<typename T>
class Test
{
public:
int getLineT()
{
T* temp = new T();
return temp->GetLine()
}
};
int main()
{
Test<derived>* temp = new Test<derived>();
cout << "Number is : " << temp->GetLine() << endl;
}
while in another file
class derived: public base
{
};
The GetLine function is implemented in base, and yes it has a virtual method that was implemented in derived. Everything until the template was working as intended. However I need to be able to create instances of the 'T' value in the template, The simple reason is that I will need to create many instances in the template class where each instance will change it's line and be able to multi track it without knowing in advance how many I have.
Getting the Error: "Invalid new-expression of abstract class type 'derived' Any suggestions on how to be capable of creating new 'T' instances when the base is abstract? or an explanation of why it 'chooses' the base version?
Edit, Adding base
'''
class Base
{
public:
Base(){}
~Base(){}
virtual bool event(const std::string name, int line) = 0;
int getLine () const {return m_line;}
protected:
int m_line;
std::string m_name;
};
class derived : public Base
{
public:
bool event(const std::string name, int line)
{
m_name = name;
m_line = line;
return true;
}
};
For the record, this works:
'''
template<typename T>
class Test_no_new
{
public:
int getLineT(T *y)
{
return y->GetLine();
};
''''
The problem is when I create an instance of T with new
Aucun commentaire:
Enregistrer un commentaire