Why Page::setMainFrame use PassRefPtr type parameter?
Look at this:void Page::setMainFrame(PassRefPtr<Frame> mainFrame). Why use PassRefPtr? Infact,change to following will work same: void Page::setMainFrame(Frame *mainFrame) 1).Look into the function source code: m_mainFrame = mainFrame; --> call "operator=(T* optr)",reference count +1 It mean that if change to "Frame *mainFrame" ,will call mainFrame->ref(); 2).Using "PassRefPtr<Frame> mainFrame": m_mainFrame = mainFrame; --> call "operator=(const PassRefPtr<T>& o)",reference count no change But,reference count was changed when the function is called: page->setMainFrame(this); --> call "PassRefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }", reference count +1 So,The "PassRefPtr<Frame> mainFrame" and the "Frame *mainFrame" cause same reference count. But use "PassRefPtr<Frame>" will call new operation!
On Nov 28, 2009, at 12:09 AM, pattin.shieh wrote:
Look at this:void Page::setMainFrame(PassRefPtr<Frame> mainFrame). Why use PassRefPtr?
Because the only call to Page::setMainFrame is inside the Frame constructor, there is no performance benefit to taking a PassRefPtr. But it’s arguably a good practice for a function that takes ownership of its argument to take a PassRefPtr even in cases where there is no performance benefit. It’s how the function states that it will keep a reference. -- Darin
participants (2)
-
Darin Adler
-
pattin.shieh