Skip to main content
GameDev.net gamedev.net
🔒 Locked

template problem

Started by Junk Detector Mar 15, 2003 at 6:22 AM 2 replies 350+ views
Original Post
Junk Detector
Junk Detector
I would like to define operator =, such as templ obj1; templ obj2; so obj1=obj2; would be correct. I can assume that class2: public class1{..} as well. Can I do that with templates ? Thanks
-||-Conscious FOX-||-PRESIDENT OF MYSELF
Junk Detector
Junk Detector
"<" constantly gets removed, but I hope you understand what I mean

Thanks for help

[edited by - Junk Detector on March 15, 2003 7:26:45 AM]
-||-Conscious FOX-||-PRESIDENT OF MYSELF
civguy
civguy
& l t ;
without spaces is <
& g t ;
without spaces is >

and
[ source ]template<class>[ /source ]
without spaces is

    template<class>    


And what you want can be done:

  
template <class T>
class temp1 {
T* member;
public:
template<class E>
temp1& operator=(const temp1<E>& rhs) {
member = rhs.member;
return *this;
}
};

The templated member function won't be created unless it's used. And when it's used, it'll be checked whether member = rhs.member assignment is possible. And it is possible if member is a pointer to rhs.member's superclass like in your example.

[edited by - civguy on March 15, 2003 8:20:27 AM]
Junk Detector
Junk Detector
Thanks ! I was able to get it working. Seems that its working like intended, so big thanks.


  
Strangely enough, If I have assignment operator in template<T>, assignment in template<E> should return the same value as before.
because I have operator which assigns objects from the same template type like

template <c> t1,t2;
t1=t2;


but now i guess previous operator can be removed

Thanks again


[edited by - Junk Detector on March 15, 2003 9:32:22 AM]
-||-Conscious FOX-||-PRESIDENT OF MYSELF

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.