CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

Operator New help More options topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 03-30-2007, 01:13 AM
Junior Member
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
John is on a distinguished road
| More
Operator New help More options

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
operator+ in derived classes More options JimLangston C++ Programming 0 03-30-2007 01:17 AM
zero shift More options Mike C Programming 0 03-30-2007 01:15 AM
problem with int_type.... More options SpreadTooThin C++ Programming 0 03-30-2007 01:09 AM
Overloading operator<< as a friend to a template class CrimzonRJ C++ Programming 1 12-26-2006 01:23 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 01:28 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board