Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
Hello everyone,

Below see a code snipped copied from
http://msdn.microsoft.com/en-us/library/efk30beh%28v=vs.80%29.aspx[^]

I have two independet questions regarding the parts in bold



class CPrimeTest
{
public:
   CPrimeTest()
      : m_pCalcNext( new CEvent( FALSE, FALSE ) )
      , m_pCalcFinished( new CEvent( FALSE, FALSE ) )
      , m_pTerminateThread( new CEvent( FALSE, FALSE ) )
      , m_iCurrentPrime( 0 )
   {   
      // Create a thread that will calculate the prime numbers
      CWinThread* pThread;
      pThread = ::AfxBeginThread( PrimeCalcProc, this, 0, 0, CREATE_SUSPENDED, NULL);
      pThread->m_bAutoDelete = FALSE; 
      pThread->ResumeThread();

      // Calcuate the first 10 prime numbers in the series on the thread
      for( UINT i = 0; i < 10; i++ )
      {
         // Signal the thread to do the next work item
         m_pCalcNext->SetEvent();
         // Wait for the thread to complete the current task
         ::WaitForSingleObject( m_pCalcFinished->m_hObject, INFINITE );
         // Print the result
         TRACE( "The value of m_iCurrentPrime is: %d\n", m_iCurrentPrime );
      }

      // Notify the worker thread to exit and wait for it to complete
      m_pTerminateThread->SetEvent();
      ::WaitForSingleObject( pThread->m_hThread, INFINITE ); 
      delete pThread;
   }



Question 1:

CPrimeTest()
: m_pCalcNext( new CEvent( FALSE, FALSE ) )
, m_pCalcFinished( new CEvent( FALSE, FALSE ) )
, m_pTerminateThread( new CEvent( FALSE, FALSE ) )
, m_iCurrentPrime( 0 )

This part is "generally unclear" to me ... can someone explain what exactly this does? With my limited programming experience I expect declaration of public member functions & variables here ... which thiiis probably is, but in a to me unclear way.

Question 2:

::AfxBeginThread

Why the :: in front? Only know this from Classname::memberFunction?
Why here? It does work without, but there must be some "readabilty" reason which I dont get.

Bonus Question:

Of course I tried googling the "::AfxBeginThread" type of statment. However, I think google allows no way of searching for special characters.
I assume this often is a problem for novice programmers when looking for specific stuff example code like "array[][]". Any suggestions to that problem?

[edit]Inline code converted to code clock - OriginalGriff[/edit]
Posted
Updated 17-May-11 20:58pm
v3

1. The 'generally unclear part' is the constructor initialization list. And it does what the name suggests. It initializes class fields before anything else from the body of the constructor is called. Have a look at google for more info. One of the links: Click[^]

2. :: before a method name means that the method is part of the global namespace. The '::' is called the 'Scope resolution operator' Have a look: Here[^]

Bonus Question:
This was actually brought up yesterday on this forum: Link[^]. There are some things google won't allow. But! There is also a nice tutorial on things you can make google do Here[^]
 
Share this answer
 
v3
Comments
tschoni 18-May-11 3:41am    
perfect answer, thanks a million times
Olivier Levrey 18-May-11 3:47am    
Very good answer. My 5.
tolw's answer is good. I will just add a note about question 2: the ::AfxBeginThread syntax.

In this case the code will compile with or without the ::. The code writer just wanted to focus on the fact that this function is not part of his class but is part of the global scope as tolw already told you.

You will find similar things about this syntax: a few people will write
C++
this->someVariable...;

Instead of directly accessing the variable:
C++
someVariable...;

Even though it would compile without this, the coder wants to show that someVariable is part of the class.

Actually, MFC naming convention prefered to prefix variable names with m_ when they are members of a class (m meaning member of course), which makes the use of this unnecessary.
I sometimes find people naming their local variables m_xxx! Please don't do that since it brings only confusion...
 
Share this answer
 
Comments
tschoni 18-May-11 4:08am    
good to know, in the example code I actually wondered what the m_ convention means. thanks a lot.
Stefan_Lang 18-May-11 5:01am    
In fact, the syntax this->someVariable is sometimes used to negate the effect of local variables overriding your class variables if they have the same name, e. g. like this:

class CMyClass {
int value;
public:
void setValue(int value) { // the parameter overrides the visibility of this->value
this->value = value; // ok, access member variable through this
}
};
Olivier Levrey 18-May-11 5:03am    
Sure. Same as :: operator for local variables/global variables.
For others who never heard of initialization lists i recommend reading
this[^]

And thanks again for the quick answer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900