1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Question Regarding Constructor Implementation

Discussion in 'C++' started by chasseml, Mar 10, 2009.

  1. chasseml

    chasseml New Member

    Joined:
    Mar 10, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have a class that has as a private data member a pointer of the same type.
    E.g. The class is "ClassA" and has as a member "ClassA* _classA".

    I am writing the copy constructor of ClassA, and would like to do a "deep copy"
    of data member _classA, e.g.:

    ClassA::ClassA(const ClassA& orig)
    : _classA(NULL)
    {
    if (orig._classA != NULL)
    {
    _classA = new ClassA(*(orig._classA));
    }
    }

    Is that allowed?

    Thanks in advance, chasse
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    _classA = new ClassA(*(orig._classA));

    would mean a cyclic reference
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes it should be OK. What happens if you try it?
     
  4. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Why u are calling infinite time means Cyclic . You have do this functionality in some other ways.

    If You wanna on stack , your code may be tackle like this

    Code:
    class ClassA
    {
      public:
       void Initialize(ClassA);
       ClassA()
       { 
         cout<<"ctor"<<endl;
       }  
       ClassA(const ClassA &);
       ~ClassA() { cout<<"Dctor"<<endl;}
        void Display() { cout<<"I am here"<<endl;}
      private:
       ClassA*  _classA; 
    };
    
    void ClassA :: Initialize(ClassA prev)
    {
         _classA = &prev;
     }
    
    ClassA::ClassA(const ClassA& orig)
    : _classA(NULL)
    {
      cout<<"copy ctor"<<endl;
      if (orig._classA != NULL)
      {
         this->_classA = (orig._classA) ;
       }
    }
    
    int main()
    {
      ClassA obj1;
      obj1.Initialize(obj1);
      ClassA obj2(obj1);
      //obj2.Display();
      return 0;
    }
    If You wanna On heap then your code may like this...

    Code:
    class ClassA
    {
      public:
       void Initialize(ClassA*);
       ClassA()
       { 
         cout<<"ctor"<<endl;
       }
      
       ClassA(const ClassA &);
       ~ClassA() { cout<<"Dctor"<<endl;}
        void Display() { cout<<"I am here"<<endl;}
      private:
       ClassA*  _classA;
    
     
    };
    
    void ClassA :: Initialize(ClassA* prev)
    {
         _classA = prev->_classA;
     }
    
    ClassA::ClassA(const ClassA& orig)
    : _classA(NULL)
    {
      cout<<"copy ctor"<<endl;
      if (orig._classA != NULL)
      {
         _classA = new ClassA();
         this->_classA = (orig._classA) ;
       }
    
    }
    
    int main()
    {
      ClassA* p1;
      p1 = new ClassA();
      p1->Initialize(p1);
      ClassA obj2(*p1);
      //obj2.Display();
      return 0;
    }
    This is just an idea . second one itself has hole...Please take care your code and prevent your code from any hole.
    Do as your requirement.
     

Share This Page