working together with operator overloading and inheritance.

This is a discussion on working together with operator overloading and inheritance. within the C++ Programming forums, part of the General Programming Boards category; base and derive classes are overloading operator '=' . Now I instantiate two objects of derive class and copy one ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    working together with operator overloading and inheritance.

    base and derive classes are overloading operator '=' .
    Now I instantiate two objects of derive class and copy one object into other.
    '=' operator for derived class gets invoked. I am expecting both base and derive class '=' should get executed.
    any solution?

    Please check the code snippet

    Code:
    #include<iostream>
    
    using namespace std;
    
    
    
    /*overload operator in base class and for derived class */
    
    
    
    class base
    {
    public:
       base& operator = (base& obj)
       {
           cout<<endl<<"In base = operator"<<endl;  
           return *this;
       }
    
    };
    
    
    class derive : public base
    {
    public:
    	derive& operator = (derive& obj)
    	{
    	   cout<<endl<<"In derive = operator"<<endl; 
    	   return *this;
    	}
    
    };
    
    
    void main()
    {
        derive obj1, obj2;
        obj1 = obj2;
    }

    o/p

    In derive = operator

    -------------------------------------------------------------------
    expected o/p

    In base = operator

    In derive = operator

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    explicitly call the operator .

    Code:
    derive& derive::operator=(derive& rhs)
    {
        base::operator=(rhs);
        cout<<endl<<"In derive = operator"<<endl; 
        return *this;
    }

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Both base and derive class using default '=' implementation then base '=' implementation will not get invoked in above code.

    So is it a thumb rule to overload derive class '=' to make sure we explicitly call base class '=' operator ?


    Thanks
    Ketan

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    No, it is not. The default assignment operator will always work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    yes, the only reason it wasn't being invoked was because you declared it, but did not call it.

    as a rule of thumb, i'd say that to the greatest extent possible, each type should handle it's own members (i.e. not touch those higher in the hierarchy). this is just my personal preference. i try to write so that default assignment & copy-construction work as desired.
    Last edited by m37h0d; 07-21-2011 at 02:34 PM.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Thanks m37h0d and Elysia!

    I made some changes in above code(just to check behavior).
    changes are 1. base class is using default '=' operator.
    2. derive class is using overloaded '=' operator.

    Code:
       
     class base
    {
       public:
       int b;
       base(int i ):b(i) {}
       /*base& operator = (base& obj)
       {
           cout<<endl<<"In base = operator"<<endl;  
           return *this;
       }*/
    
    };
    
    
    class derive : public base
    {
    public:
    	int d;
    	derive(int j, int k):d(j),base(k){}
    	derive& operator = (derive& obj)
    	{
    	   base::operator = (obj);
    	   cout<<endl<<"In derive = operator"<<endl; 
    	   d = obj.d;
    	   return *this;
    	}
    	
    };
    
    
    void main()
    {
       derive obj1(10,20), obj2(30,40);
       obj1 = obj2;
     }

    Now if I dont add undelined code statement base class default = implementation is not invoked. I think while overloading derive class = one need to make sure he calls base:perator = (base&)

    Thanks,
    Ketan

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Since you are overloading an operator, it is the same as calling an overloaded function. That is, only the derived's instance of the function will be called.
    So if you want to invoke the default copying behavior of base, then yes, you must call base's assignment operator explicitly. Otherwise you have to code the assignment of any inherited variables, too.

    Also, void main shall be int main.
    Last edited by Elysia; 07-22-2011 at 01:58 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading not working
    By jack_carver in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2009, 12:50 AM
  2. [B]Overloading Functions In Inheritance[/B]
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2005, 10:50 AM
  3. Inheritance operator overloading
    By global in forum C++ Programming
    Replies: 9
    Last Post: 12-21-2004, 06:03 AM
  4. Inheritance and Overloading
    By XSquared in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2004, 07:40 PM
  5. Inheritance with pointers:Overloading?
    By katie in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2004, 01:26 PM

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