c++ - using template classes as method parameters -
i've 2 template classes: class1< s > , class2< t >. in class2< t >, there's method has parameter pointer object of class1< s >. should re-define class2< t > class2< s, t >? or there other best solution? problem, might have new methods referring objects of other template classes parameters. therefore, avoid having sth. like: class2< s, t , u ...>
template < class s > class class1{ public: ... private: ... }; template < class t > class class2{ public: ... class2<t> * dosomething(class1<s> * ); ... private: ... }; template < class s, class t > class class2{ public: ... class2<t> * dosomething(class1<s> * ); ... private: ... };
the type of object class2::dosomething
acts upon should not have part of class2
's type. make class2::dosomething()
member function template:
template < class t > class class2{ public: ... template<class s> class2<t> * dosomething(class1<s> * ); ... private: ... };
edit:
defining member function template easy enough if inline, can't or don't want that, , funky syntax comes in play. here's complete example illustrates both how define member function template, , how call it. i've changed names little easier read & follow.
template<class footype> class foo { }; template<class bartype> class bar { public: template<class footype> bar<bartype>* dosomething(foo<footype>* foo); }; template<typename bartype> template<typename footype> bar<bartype>* bar<bartype>::dosomething(foo<footype>* foo) { return 0; } int main() { foo<unsigned> foo_1; bar<double> bar_1; bar<double> * bar_copy = 0; bar_copy = bar_1.dosomething<unsigned>(&foo_1); return 0; }
Comments
Post a Comment