1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Operator New help More options

Discussion in 'C++' started by imported_John, Mar 30, 2007.

  1. imported_John

    imported_John New Member

    Joined:
    Sep 30, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys,

    Quick question i have this code and what i want to do is create a deep copy of class B. Now I tried doing this with the new operator and pointers. Here's is the orignal

    ---------------- Original Copy-----------------------
    Code:
    #include <iostream> 
    using namespace std; 
    
    class A 
    { 
      int n ; 
      public: 
       A():n(0) 
       { 
       } 
       A(int x):n(x) 
       { 
        n = x; 
       } 
    
    
       void print() 
       { cout << n<<"\n\n"; 
       } 
    
        A(const A& objectCopy){ 
    
                n = objectCopy.n;    // copy constructor 
        } 
    
       }; 
    
       class B 
       { 
        A * a; 
    
         public: 
    
        B(A & x) 
          { 
          a = &x;      // Here is the problem so I implemented a new command 
            } 
    
          void print () 
           { 
             a->print(); 
             } 
           B(const B& copy){    // Class B copy constructor 
              a = copy.a; 
    
             } 
             const B &operator=(const B x){ 
              a = x.a;             // Operator 
             } 
             B::~B(){ 
                 delete a; 
    
                    } 
            }; 
    
    
          int main() 
          { 
    
            A a(5); 
            A a1(7); 
    
            a1.print(); 
    
            B b(a1); 
            b.print(); 
             B c(a); 
             b.print(); 
             b = c; 
    
    
            b.print(); 
            cout << endl; 
            int trick; 
            cin >> trick; 
            return 0; 
          } 
    //--------------------End of Orignal Copy-----------------------

    ----Version altered with new command in class B--------

    Code:
    #include <iostream> 
    using namespace std; 
    
    
    class A 
    { 
      int n ; 
      public: 
       A():n(0) 
       { 
       } 
       A(int x):n(x) 
       { 
        n = x; 
       } 
    
    
       void print() 
       { cout << n<<"\n\n"; 
       } 
    
    
        A(const A& objectCopy){ 
    
    
                n = objectCopy.n;    // copy constructor 
    
    
        } 
    
    
       }; 
    
    
       class B 
       { 
        A * a; 
    
    
         public: 
    
    
        B(A & x) 
          { 
          a = new A(x);    //New command 
    
    
            } 
    
    
          void print () 
           { 
             a->print(); 
             } 
           B(const B& copy){    // Class B copy constructor 
              a = copy.a; 
    
    
             } 
             const B &operator=(const B x){ 
              a = x.a;             // Operator 
             } 
             B::~B(){ 
                 delete a; 
    
    
                    } 
            }; 
    
    
          int main() 
          { 
    
    
            A a(5); 
            A a1(7); 
    
    
            a1.print(); 
    
    
            B b(a1); 
            b.print(); 
             B c(a); 
             b.print(); 
             b = c; 
    
    
            b.print(); 
            cout << endl; 
            int trick; 
            cin >> trick; 
            return 0; 
            } 
    //---------------------------------------------------------------------
    Works fine but say if i change main() so it looks like this


    ------------------ Aletered main -----------------

    Code:
    int main() 
          { 
    
    
            A a(5); 
    
    
            B b = a; 
    
    
            { 
    
    
                A a1 (7); 
                B b1 =a1; 
                b = b1; 
            } 
    
    
            b.print(); 
            cout << endl; 
            int trick; 
            cin >> trick; 
            return 0; 
            } 
    --------------------- End of altered main--------------------

    I get a bunch of junk so I found out i need to make a deep copy of B.


    This is what i came up with

    int *p;
    p = new int();
    *p = a;

    I came up with invalid conversion from 'A*' to 'int'. I don't get why this wouldn't work.

    thank you
    john

    any help would be appreciated.
     

Share This Page