语法混淆:类成员函数声明 [英] Syntax confusion: Class member function declaration

查看:134
本文介绍了语法混淆:类成员函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

下面看到的是从
复制而来的代码片段 http://msdn.microsoft.com/en-us/library/efk30beh%28v = vs.80%29.aspx [ ^ ]

关于粗体
中的部分,我有两个独立的问题


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;
   }




问题1:

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

这部分对我来说通常不清楚"……有人可以解释一下这到底是做什么的吗?由于我有限的编程经验,我希望声明公共成员函数&.这里的变量...可能是,但是对我来说还不清楚.

问题2:

:: AfxBeginThread

为什么::放在前面?仅从Classname :: memberFunction知道吗?
为什么在这里?它确实可以在没有的情况下工作,但是必须有一些我无法理解的可读性"原因.
奖金问题:

当然,我尝试谷歌搜索":: AfxBeginThread"类型的陈述.但是,我认为Google不允许搜索特殊字符.
我认为这对于新手程序员在查找诸如"array [] []"之类的特定示例代码时通常是个问题.对这个问题有什么建议吗?

[edit]将内联代码转换为代码时钟-OriginalGriff [/edit]




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]

推荐答案

1. 通常不清楚的部分"是构造函数初始化列表.确实如其名所示.它会在调用构造函数主体中的其他任何东西之前初始化类字段.看看谷歌更多的信息.链接之一:单击 [此处 [ ^ ]

奖励问题:
这实际上是昨天在此论坛上提出的:链接 [ ^ ]. Google不允许您执行某些操作.但!还有一个很好的教程,介绍了您可以让Google做的事情此处 [
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[^]


tolw 的答案很好.我将仅添加有关问题2的注释:::AfxBeginThread语法.

在这种情况下,无论是否使用::,代码都可以编译.正如 tolw 早已告诉您的那样,代码编写者只想关注以下事实:该函数不是其类的一部分,而是全局范围的一部分.

您会发现关于this语法的类似信息:会有一些人写
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
this->someVariable...;


而不是直接访问变量:


Instead of directly accessing the variable:

someVariable...;


即使在没有this的情况下进行编译,编码人员也希望证明someVariable是该类的一部分.

实际上,当MFC命名约定是类的成员时,它们更喜欢在变量名前加上m_(m的意思是 member ),这使得不必使用this. /> 有时我发现有人在命名他们的 local 变量m_xxx!请不要这样做,因为它只会带来混乱...


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...


对于从未听说过初始化列表的其他人,我建议阅读
[
For others who never heard of initialization lists i recommend reading
this[^]

And thanks again for the quick answer.


这篇关于语法混淆:类成员函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆