adir
adir

Reputation: 1287

Help with copying an object in C++ mfc

i have this code which doesnt work:

void CAnalyzerIF::SetData(int i_iIters, const CParameterSet* i_CParameterSet)
{
const CParameterSet ParameterSet=(*i_CParameterSet);  //thous are the problem 
     const CParameterSet* pCParameterSet=&ParameterSet;     //lines
// ... now im sending the pCParameterSet to other class
}

ive tried to configure them in the header but the it just doest compile. and now it do compile but it fail on run time. maybe i should copy it in some binary way.... i dont know. thanks for the help

Upvotes: 1

Views: 298

Answers (2)

Bo Persson
Bo Persson

Reputation: 92341

If you just send a pointer to a const object, you really don't need to copy it first. Just pass the parameter on to the next function.

If the class you send the pointer to happens to save it and try to access your copy later, you will just be in more trouble for passing a pointer to a local object that is destroyed at the end of the function.

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33272

If you need to clone the i_CParametreSet you need:

  1. The class CParamterSet declaring a "copy constructor"
  2. Copy the instance data by hand ( probably does not works due to protected private members )

You should also pay attention on the class content and decide if you need a Shallow Copy or a Deep Copy of the class instance.

Upvotes: 2

Related Questions