|
please explain in detail.
if possible give any example.
|
|
|
|
|
// shallow copy
Fxn(Class1 Obj1)
{
m_pPointer = Obj1.m_pPinter; // just copying the where the pointer points to
}
// Deepcopy
Fxn(Class1 Obj1)
{
m_pPinter = new int[obj1.size]
memcpy(m_pPointer,Obj1.m_pPinter,Obj1.size); // Allocating memory and copying its contents
}
-Sarath
|
|
|
|
|
|
A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator.
Deep copy needs,
If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required.
A class that requires deep copies will generally need:
i )a destructor to delete the dynamically allocated memory.
ii) a copy constructor to make a copy of the dynamically allocated memory.
iii) an overloaded assignment operator to make a copy of the dynamically allocated memory.
Cheers
"Peace of mind through Technology"
|
|
|
|
|
Ganesh_T wrote: A shallow copy of an object ...
Exactly the same as the link I found.
Maxwell Chen
|
|
|
|
|
|
Nibu thomas wrote: Is it a Copy/Paste?
It's a deep copy!
Maxwell Chen
|
|
|
|
|
|
ROTFL.
OMG... Im a geek. I actually
a) got the joke
b) found it insanely funny.
Well... I can live with that
|
|
|
|
|
Maxwell Chen wrote: It's a deep copy!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
I want to add a progress bar to my dialog window which start in the thread function and ends after some process can u help me with this.
-------------------------------------------
BOOL CStatusDlg::OnInitDialog()
{
CDialog::OnInitDialog();
HANDLE hr;
hr = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadProc,this,0,0);
return TRUE;
}
---------------------------------------------------
UINT WorkerThreadProc(LPVOID Param)
{
CStatusDlg* status = (CStatusDlg *)Param;
// I want the progress bar to start here
INDX.startIndex();
// I want the progress bar to stop here
return true;
}
Thanks
|
|
|
|
|
U have to call the StepIt function for the progress bar to progress each step..
nave
|
|
|
|
|
Thanks for ur response. can u please give me the code to do it? i dont want to display it in status bar
-- modified at 5:25 Friday 19th May, 2006
|
|
|
|
|
UINT WorkerThreadProc(LPVOID Param)
{
CStatusDlg* status = (CStatusDlg *)Param;
status->m_ProgressBar.SetRange( 0, 100 );
// progress barstart here
for( int i =0;i < 100; i++ )
{
status->m_ProgressBar.StepIt();
}
// Iprogress bar stop here
return true;
}
nave
|
|
|
|
|
Mmmhhh, won't have problems with that method ? You are accessing an MFC object from two different threads. I think you will get troubles with (but I'm not sure, I never pass MFC objects across different threads).
Take a look here[^], specifically at the Windows Handle Maps section.
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
I have read that we can pass C++ object to other thread which is derived from CObject. Also we must ensure the existence of the object.
nave
|
|
|
|
|
Hi naveen i tried this code but its not working dont know what is the problem
|
|
|
|
|
|
i have a timer. on click of a button this timer starts.i want the progress bar to start on click of the button and end when the timer is killed
UINT WorkerThreadProc(LPVOID Param)
{
CStatusDlg* status = (CStatusDlg *)Param;
// I want the process bar to start here
INDX.startIndex();
// I want the process bar to stop here
KillTimer(status->m_hWnd, ELAPSED_TIMER);
return true;
}
how to do this?
|
|
|
|
|
Then why r u creating a thread..U can step the progress bar in the OnTimer()
funtion..isn't it?
nave
|
|
|
|
|
this is my ontimer() function. how can i step progress bar here? how do i ensure that the progress bar stops once timer is killed
void CStatusDlg::OnTimer(UINT nIDEvent)
{
CStatusDlg* status = (CStatusDlg *)Param;
if(nIDEvent == ELAPSED_TIMER)
{
time(&lStopTime);
cteElapsedTime = CTimeSpan(lStopTime-lStartTime);
CString csElapsedTime;
csElapsedTime.Format("%02d:%02d:%02d",
cteElapsedTime.GetHours(),
cteElapsedTime.GetMinutes(),
cteElapsedTime.GetSeconds());
if(IsWindowVisible())
{
m_TIME.SetWindowText(csElapsedTime);
}
}
|
|
|
|
|
Before calling the SetTimer initialize the progress bar
m_ProgressBar.SetRange( 0, 100 );<br />
SetTimer( ELAPSED_TIMER );
now in the timer function
void CStatusDlg::OnTimer(UINT nIDEvent) <br />
{<br />
if(nIDEvent == ELAPSED_TIMER)<br />
{<br />
m_ProgressBar.StepIt();<br />
time(&lStopTime);<br />
cteElapsedTime = CTimeSpan(lStopTime-lStartTime); <br />
CString csElapsedTime;<br />
csElapsedTime.Format("%02d:%02d:%02d", <br />
cteElapsedTime.GetHours(),<br />
cteElapsedTime.GetMinutes(),<br />
cteElapsedTime.GetSeconds());<br />
if(IsWindowVisible())<br />
{<br />
m_TIME.SetWindowText(csElapsedTime);<br />
}<br />
}<br />
}
nave
|
|
|
|
|
Thank you naveen for your help. i am getting following errors in
CStatusDlg* status = (CStatusDlg *)Param;
m_Progress_Bar.SetRange( 0, 100 ); time(&lStartTime);
SetTimer(status->m_hWnd,ELAPSED_TIMER, 1000, NULL);
if i comment that line i dont get any errors. in ontimer() function i am using m_ProgressBar.StepIt(); directly but here its not giving any error. what cud be wrong?
error C2065: 'm_Progress_Bar' : undeclared identifier
error C2228: left of '.SetRange' must have class/struct/union type
Thanks
|
|
|
|
|
ho man... m_ProgressBar is the control variable for the progress control
replace m_ProgressBar with the control varaiable u created for the progress bar..
nave
|
|
|
|
|
hey sorry typing mistake. my control variable is m_Progress_Bar.
in ontimer() function this is not giving any error i.e
m_Progress_Bar.StepIt();(no error here).
but this is giving error
m_Progress_Bar.SetRange( 0, 100 );
if i comment m_Progress_Bar.SetRange( 0, 100 ); then it gives no error
|
|
|
|