Serving the Quantitative Finance Community

 
User avatar
horacioaliaga
Topic Author
Posts: 0
Joined: August 21st, 2005, 3:30 pm

C++ question - how to write a virtual function that returns vector<T>

October 17th, 2011, 3:23 pm

If not possible, what would be a good work around?
 
User avatar
fgarelli
Posts: 0
Joined: December 25th, 2009, 7:59 pm

C++ question - how to write a virtual function that returns vector<T>

October 17th, 2011, 4:39 pm

Yes it's possible to write something like :class vectorReturn{public: vectorReturn(); virtual std::vector<int> getFilledVector(); virtual void getFilledVectorNoCopy(std::vector<int>& vec); virtual void fillTheVector();private: std::vector<int> m_toFill;};vectorReturn::vectorReturn(){ }std::vector<int> vectorReturn::getFilledVector(){ return m_toFill;}void vectorReturn::fillTheVector(){ m_toFill.push_back(45); m_toFill.push_back(99); m_toFill.push_back(123);}void vectorReturn::getFilledVectorNoCopy(std::vector<int>& vec){ //Fill the vector with your data}If the vector is small returning a vector can be fine but if you want to be sure to stay away from any copy on return I think it would be better to pass the vector you want to fill getFilledVectorNoCopy(std::vector<int>& vec)cheers
 
User avatar
lballabio
Posts: 0
Joined: January 19th, 2004, 12:34 pm

C++ question - how to write a virtual function that returns vector<T>

October 17th, 2011, 6:03 pm

I think the OP was asking whether you can write something liketemplate <class T> virtual std::vector<T> foo();but it's hard to tell from the question. Horacio, can you elaborate a bit?
 
User avatar
fgarelli
Posts: 0
Joined: December 25th, 2009, 7:59 pm

C++ question - how to write a virtual function that returns vector<T>

October 17th, 2011, 6:39 pm

Yes maybe sharing more information would be more useful
 
User avatar
nosbor
Posts: 0
Joined: December 10th, 2006, 12:54 pm

C++ question - how to write a virtual function that returns vector<T>

October 17th, 2011, 6:39 pm

you mean something like this ?
 
User avatar
DevonFangs
Posts: 0
Joined: November 9th, 2009, 1:49 pm

C++ question - how to write a virtual function that returns vector<T>

October 18th, 2011, 7:50 am

QuoteOriginally posted by: lballabioI think the OP was asking whether you can write something liketemplate <class T> virtual std::vector<T> foo();but it's hard to tell from the question. Horacio, can you elaborate a bit?I think he meant this.You can't have template virtual member methods for the simple reason that a template member is not really a member. Its instances are. Methods of a template class can be virtual though, so maybe you could:1. make the original class a template2. use a nested template classYou can find a workaround here.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ question - how to write a virtual function that returns vector<T>

October 18th, 2011, 4:40 pm

QuoteOriginally posted by: horacioaliagaIf not possible, what would be a good work around?Whatever, It will be very inefficient (virtual and return by value (copy)). Specification is not unambiguous. //Vaguely remember trying template Visitor one .. did not work. I suspect this won't work either (vtabl and templates don't mix well).
Last edited by Cuchulainn on October 17th, 2011, 10:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

C++ question - how to write a virtual function that returns vector<T>

October 18th, 2011, 5:25 pm

Horacio,I think it's a kind of factory you want??
Last edited by Cuchulainn on October 17th, 2011, 10:00 pm, edited 1 time in total.