Thread: Overloaded = operator problems

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    14

    Question Overloaded = operator problems

    Code:
    #include <iostream>
    using namespace std;
    
    class Point
    {
    private:
       int* ptr;
    public:
       Point(int val) {ptr = &val;}
       ~Point() {delete ptr;}
       void GetVal() const {cout << *ptr << endl;}
       void NewLoc(int loc) {delete ptr; ptr = &loc;}
       void SetVal(int val) {*ptr = val;}
       void printaddr() const {cout << ptr << endl;}
       int* GiveVal() const {return ptr;}
       const Point& operator=(const Point& rhs)
       {
          if(&rhs != this)
          {
             delete ptr;
             ptr = new int;
             *ptr = *(rhs.GiveVal());
          }
          return *this;
       } 
    };
    
    int main()
    {
       int myInt = 90, yourInt = 50;
       Point p1(myInt), p2(yourInt);
       p1.GetVal();
       p1.printaddr();
       p2.printaddr();
       //p2 = p1;
       p2.printaddr();
       //cout << *(p2.GiveVal()) << endl;
       //cout << *(p1.GiveVal()) << endl;
       p2.GetVal();
       p1.GetVal();
       p2.SetVal(1267);
       p1.GetVal();
          return 0;
    }
    The program above is an exercise involving the = operator and a pointer as a data member. When the = operator is used, new memory is allocated for the pointer in the assigned to class, and the value pointed to by the assigning class is stored in this heap memory, to avoid multiple objects pointing to the same place.
    However, after the commented out line p2 = p1, the values pointed to by the pointers of both objects seem to change unaccountably, and I don't understand why. The other commented out lines seem to change the values further even though they are intended to fetch values... After this, p2's value is modified, and p1's value also changes, even though the two objects should now not be pointing to the same memory addresses!
    The code is rather clumsy, for example there are several member functions that all do similar things, but these were implemented in an attempt to find out what was going wrong.
    I would be very grateful if someone with more expertise could assist a bemused novice, and apologise for wasting your time if the mistake is glaringly obvious and simple.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Point(int val) {ptr = &val;}

    by passing the parameter by value, you are setting the pointer to a variable local to the function (ie: a copy). you would need to pass a pointer or a reference to get that to work.

    >> void NewLoc(int loc) {delete ptr; ptr = &loc;}

    the problem here is that you are deleting the pointer without knowing whether it was allocated by operator new or not.

    finally, you don't define a copy constructor which would be needed to handle the special requirements for the class ( ie: the code in operator = ).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    Thank you! How can I test to see if heap storage has been allocated?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you can't. you will need to keep track of that 'manually'.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    Ah. Well, after changing the constructor's parameter type to a reference to an int, all the problems with incorrect values for the pointers were corrected. Thank you very much, I doubt I would have spotted that the local copy was to blame.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  4. Cannot resolve overloaded function
    By Mithoric in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2003, 03:40 AM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 02:14 AM