Thread: operator = overloading

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    operator = overloading

    in this example class what will happen to the year of birth? Will it have a garbage value?


    Code:
    Car* m_pCar;
    >     double m_salary;
    >     const int m_yearOfBirth;
    >public:
    >     Person(int yearOfBirth, Car* pCar, double salary):
    >      m_yearOfBirth(yearOfBirth){
    >           m_pCar=pCar;
    >           m_salary=salary;
    >      }
    >
    >      Person(const Person& other): m_yearOfBirth(other.m_yearOfBirth){
    >           m_pCar=other.m_pCar;
    
    This will make the two persons share the same car.   
    
    >           m_salary=other.m_salary;
    >      }
    >      
    >      const Person& operator=(const Person& other){
    
    So what happend to the year of Birth? 
    
    
    >      m_pCar= other.m_pCar;
    > m_salary= other.m_salary;
    >      }
    >
    >      ~Person(){
    >           delete m_pCar;
    >      }
    >

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: operator = overloading

    Originally posted by noob2c
    So what happend to the year of Birth?
    In operator=?Nothing.yearofbirth is const,since the constructor call and till the destructor call.

    Instead you should take care of this
    > m_pCar= other.m_pCar;
    Uh,thats wrong,now two pointer point to one memory location.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    what do u mean that nothing happens?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "in this example class"

    What example class?? I don't see a class anywhere in your code.

    yearOfBirth will have whatever value you use as a parameter when calling the Person constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 03:26 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM