Message: previous - next
Month: January 2012

Re: [trinity-devel] C++ Question

From: Darrell Anderson <humanreadable@...>
Date: Mon, 16 Jan 2012 10:52:22 -0800 (PST)
>       Is'nt that "else" keyword adding
> ambigiusity, reason why this thread 
> exists? Furthermore: depending of the compiler and
> optimisation, I suppose 
> that code could be added because the "else" keyword
> existance.

I'm not qualified to answer your coding question, but with reference to why this thread exists, I never stated my reason. :) My reason is I was browsing the code of tdelibs/kio/kio/kdirwatch.cpp in the hopes of seeing something obvious to help resolve bug report 756. As I am learning C++, one of the functions in that cpp caught my attention. I wanted to know how multiple return statements within a function operate. Here is the particular snippet from line 707:

// setup INotify notification, returns false if not possible
bool KDirWatchPrivate::useINotify( Entry* e )
  e->wd = 0;
  e->dirty = false;
  if (!supports_inotify) return false;

  e->m_mode = INotifyMode;

    mask |= IN_MODIFY|IN_ATTRIB;
    mask |= IN_ONLYDIR;

  // if dependant is a file watch, we check for MODIFY & ATTRIB too
  for(Entry* dep=e->m_entries.first();dep;dep=e-> {
    if (!dep->isDir) { mask |= IN_MODIFY|IN_ATTRIB; break; }

  if ( ( e->wd = inotify_add_watch( m_inotify_fd,
        TQFile::encodeName( e->path ), mask) ) > 0 )
    return true;

  if ( e->m_status == NonExistent ) {
    if (e->isDir)
      addEntry(0, TQDir::cleanDirPath(e->path+"/.."), e, true);
      addEntry(0, TQFileInfo(e->path).dirPath(true), e, true);
    return true;

  return false;

There are four return statements in that function, two true and two false. I have been told that a function stops executing upon any return statement. That is good enough for me. If that was incorrect then I would read the snippet as always returning false because that is the last statement in the function. I did not think that was correct and my texts provided me no clues. Therefore I asked the question to be sure. Now I know that any of three return statements prior to the final return statement will cause the function to exit and return that assigned boolean value.

For those itching to hack some code and become a hero, bug report 756 identifies several items when Konqueror fails to update the file pane when a file changes state.