Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Question Regarding Constructor Implementation (http://www.go4expert.com/forums/question-regarding-constructor-t16498/)

chasseml 10Mar2009 22:55

Question Regarding Constructor Implementation
 
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

shabbir 11Mar2009 09:29

Re: Question Regarding Constructor Implementation
 
_classA = new ClassA(*(orig._classA));

would mean a cyclic reference

xpi0t0s 11Mar2009 14:23

Re: Question Regarding Constructor Implementation
 
Yes it should be OK. What happens if you try it?

asadullah.ansari 16Mar2009 15:31

Re: Question Regarding Constructor Implementation
 
Quote:

Originally Posted by chasseml (Post 44194)
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

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.


All times are GMT +5.5. The time now is 14:27.