virtual template function is in working c++ ????
-
lunes, 05 de marzo de 2012 14:58
hi All after so much surfing and cominng across various opinions I just want to know a clear idea about the following problem.
I've a base class with template and a dervied class of course with a template.Then can some of the functions declared virtual in base class and reimplemented in derived class work in c++ ..??
ex:
template<class Type>
class Hello{
public:virtual Type foo(Type jk){
cout<<"jk is in Base class "<<jk<<endl;
return jk;
}};
template<class Type>
class HelloD:public Hello<Type>{
public:
Type foo(Type jk){
cout<<"jk is in derived class "<<jk<<endl;
return jk;
}};
But I see the above code works with Qt compiler.Which one the pure virtual or both abstract and virtual base class template are not allowed in c++ .
regards
simbu
Todas las respuestas
-
lunes, 05 de marzo de 2012 15:20
-
lunes, 05 de marzo de 2012 15:52
okk..I want to create a template base class A and a template dervied class B , derived from A..I simply want to have some member functions of A as virtual functions using template parameter and I want to reimplement them in class B differently .Example is as shown in my first post.I'm asking is this legitimate to do in c++..??
If no why and if yes then will it be same with pure virtual functions..??
regards
simbu
-
lunes, 05 de marzo de 2012 16:11
You haven't shown how you use these classes, or said what you expect to happen.
But the answer to your underlying question is: Yes, functions in a template class can be virtual.
David Wilkinson | Visual C++ MVP
- Propuesto como respuesta Helen ZhaoModerator lunes, 12 de marzo de 2012 7:51
- Marcado como respuesta Helen ZhaoModerator lunes, 12 de marzo de 2012 8:02
-
lunes, 05 de marzo de 2012 18:55
A simple concept we use template to implement generic function and classes . But default behaviour of classes and function always persist and it doesn't matter how you are implementing your class .
Thanks
Rupesh Shukla
- Propuesto como respuesta Helen ZhaoModerator lunes, 12 de marzo de 2012 7:51
- Marcado como respuesta Helen ZhaoModerator lunes, 12 de marzo de 2012 8:02
-
miércoles, 07 de marzo de 2012 7:24Moderador
Hi simbu,
I have tried the codes you posted in VS2010 SP1 on my side. And in main function of a Win32 Console application, I added codes in main function as follows:
Hello<int>* hello=new Hello<int>(); HelloD<int>* hellod=new HelloD<int>(); int a=hello->foo(3); int b=hellod->foo(5);
After running the application, I saw the following result of Console Window:
So we know that we can program like this in C++. If we declare a pure virtual function in Base class like this:
public: virtual Type foo(Type jk)=0;
Then the Base class can not be initialized, we can still get result 5 from derieved class with the codes above.
I hope this reply is helpful to you. If you have any questions, please feel free to let me know.
Have a nice day!
Helen Zhao [MSFT]
MSDN Community Support | Feedback to us
- Editado Helen ZhaoModerator lunes, 12 de marzo de 2012 8:01
- Propuesto como respuesta Helen ZhaoModerator lunes, 12 de marzo de 2012 8:02
- Marcado como respuesta Helen ZhaoModerator lunes, 12 de marzo de 2012 8:02

