指针赋值的含义 [英] Meaning of pointer assignment

查看:103
本文介绍了指针赋值的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我是c ++编程的新手。任何人都可以向我解释以下陈述的含义。

  unsigned   long   __ stdcall  CPlaybackDlg :: ControlExecuteThreadEntry( void  * pParent)
{
CPlaybackDlg * pTmp =(CPlaybackDlg *)pParent;
}



其中CPlaybackDlg派生自CDialog。



提前完成Thanx

解决方案

 CPlaybackDlg * pTmp =(CPlaybackDlg *)pParent; 





这是一个 C -like void指针 CPlaybackDlg 对象一。



这样的声明你告诉编译器:我知道我在做什么:我知道那个 pParent 尽管是 void * 实际上是 PlaybackDlg * ,因此你必须处理它



这样的习惯用法经常用于将参数传递给线程函数(例如,参见此MSDN示例 [ ^ ])


hi,

pParent是一个void指针,即一个可以指向任何类型对象的指针。void指针不知道它指向的对象类型,所以void指针必须首先被显式地转换为另一个指针类型才能被解除引用。



你想要一个CPlaybackDlg类型的指针pTmp,它应该指向同一个对象pParent。所以pParent通过在它前面放置(CPlaybackDlg *)来类型化CPlaybackDlg并且这个类型转换用于初始化pTmp。



了解有关void指针的更多信息这里 [ ^ ]

关于类型转换的一些信息 here [ ^ ]



希望这会有所帮助。


还要注意,如果你不做任何事情指针,此方法没有任何用处。这就像在做什么



  void  blah()
{
int doNothing = 42 ;
}


Hi friends,

I am new in c++ programming. Can anybody explain me the meaning of below statements.

unsigned long __stdcall CPlaybackDlg::ControlExecuteThreadEntry(void *pParent)
{
	CPlaybackDlg *pTmp = (CPlaybackDlg*)pParent;
}


where CPlaybackDlg is derived from CDialog.

Thanx in advance

解决方案

CPlaybackDlg *pTmp = (CPlaybackDlg*)pParent;



That is a C-like cast of a void pointer to a CPlaybackDlg object one.

With such a statement you are telling to the compiler: "I know what I am doing: I know that pParent in spite of being a void * is really a PlaybackDlg*, hence you have to handle it consequently".

Such a idiom is frequently used for passing parameter to thread functions (see, for instance this MSDN example[^]).


hi,
the pParent is a void pointer i.e. a pointer which can point to objects of any type.The void pointer does not know what type of object it is pointing to, so the void pointer must first be explicitly cast to another pointer type before it can be dereferenced.

And you want a pointer pTmp of type CPlaybackDlg which should point at the same object as pParent . So pParent is typecast to type CPlaybackDlg by placing (CPlaybackDlg*) in front of it and this typecast is used to initialize pTmp .

know more about void pointers here[^]
some info on typecasting here[^]

hope this helps .


Also be aware that if you don''t do anything with the pointer, nothing useful happens in this method. It is like doing

void blah()
{
  int doNothing = 42;
}


这篇关于指针赋值的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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