CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Jan 2008
    Posts
    6

    still need HELP::Operator Overloading + Constructors

    thnx a ton for the earlier reply to my post regarding constructors.......it helped me a lot


    i have another query......in the following snippet the overloaded assignment operator=(with const argument as reference to object) is invoked everytime i assign either a const* or an int to the object without checking its type.

    however the code doesnt compile when i remove the const prefix in the overloaded assignment operator argument and it gives me a type mismatch error.

    Nevertheless if i change the argument from reference to object to just an object i.e.
    from
    operator=(abc& ref)
    to
    operator=(abc ref)
    ......the code compiles well.........why doesnt it give the type mismatch error now????

    please help me on this issue?........any links to furthur reading material as well on similar issue would be greatly appreciated


    Code:
    #include<iostream>
    using namespace std;
    
    class abc{
        
    public:
            abc(){};
    
           abc(char* word){
                 MemberVar = word;
          } 
    
    	   abc(int num){
    	   
    
    	   }
    
    	abc& operator=(const abc& ref)
    	{
    		return(*this);
    	}
    
    private:
            char *MemberVar;
    };
    
    int main(){
            abc object("Hello");
    
            abc anotherObject;
            
      	anotherObject = "i Know why this works now!!!";//
    
    	anotherObject = 8;//But i dunno why this compiles          
    
    	return 0;
    }
    Last edited by DivineBlade; January 29th, 2008 at 12:20 AM. Reason: Not yet clarified

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured