Templates AND operator overloads

This is a discussion on Templates AND operator overloads within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use a template with an assignment operator overload. I want to do it function style, not class ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Templates AND operator overloads

    I'm trying to use a template with an assignment operator overload.

    I want to do it function style, not class template or there is no point.

    Also, I think it is trying to use the default assignment operator...

    If I move the mouse cursor over a variable, it doesn't do anything, but if I comment out the template line, it recognizes a variable but won't compile unless I remove teh references to the template.

    It never runs my overload, I know that because if I have assert(1<0);, it won't crash. If I tell it to cout something, it won't work either.

    Here's a simplified version of my code.

    Code:
    #include <iostream.h>
    //#include <string>
    
    class wimpyclass
    {
    public:
    	wimpyclass();
    	~wimpyclass();
    	void *data;
    	int getdata();
    	template <class unknowndatatype>
    	const wimpyclass & operator = ( const unknowndatatype & rhs );     // assign unknowndatatype
    };
    
    wimpyclass::wimpyclass()
    {
    	int x = 0;
    	data = &x;
    }
    
    wimpyclass::~wimpyclass()
    {
    	return;
    }
    
    template <class unknowndatatype>
    const wimpyclass& wimpyclass::operator =(const unknowndatatype & rhs)
    //postcondition: normal assignment via copying has been performed
    {
        if ( (data != (void*)&rhs) && (this != &rhs) )                             // check aliasing
        {
            data = (void*)&rhs;										// assign the new void pointer data
    		//data = const_cast<void*>(&rhs);								// assign the new void pointer data
        }
    	else
    		cerr << "\n\nCannot assign to itself\n\n";
    
        return *this;
    }
    
    int wimpyclass::getdata()
    {
    	return *(int*)&data;
    }
    
    
    int main()
    {
    	wimpyclass junk1;
    	junk1 = 50;
    	junk1 = 'a';
    	cout << junk1.getdata() << endl << *(int*)&junk1.data << endl;
    	
    	return 0;
    }

    I have the void *data public so that I can make sure it is working... and it isn't assigning.

    However, if I remove the template and replace those with ints, it will work for ints.

    So is it possible to get a template to work with an operator overload?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    Looks like a pretty cool MSVC bug!!

    I looked at the disassembly and the compiler doesn't even generate code for the assignments - debugger just skips right over em....

    I did find a work around though - you have to implement the method inline - within the class declaration. The compiler will then show you some errors in the operator=() code! As if your version doesn't even get compiled.

    Also, getdata() can be done a little easier by simply casting data to an int.

    You could also use "reinterpret_case<void*>()" for your void* casting just to be hip.

    gg
    Last edited by Codeplug; 04-08-2003 at 11:53 AM.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    if I remove the template part and change uknowndatatype to int, it works.t hanks tho

    [edit]
    Eh, sorry... I was using a different version than the one I posted

    My version worked, that one had errrors.
    Last edited by Trauts; 04-09-2003 at 08:54 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21