简体   繁体   English

“ this”关键字是什么意思?

[英]“this” keyword, what does this mean?

I've googled the keyword "this", and most of them gave the similar examples. 我用谷歌搜索了关键字“ this”,其中大多数给出了类似的例子。

http://www.geeksforgeeks.org/this-pointer-in-c/ http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm http://www.geeksforgeeks.org/this-pointer-in-c/ http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm

When I ran into this, 当我遇到这个

Token::~Token() {
if(this == nullptr) { return; }
.... }

It just doesn't make sense. 只是没有意义。 What does "this" point to? “这个”指的是什么? If it points to 'token', how does it do that? 如果它指向“令牌”,它是如何做到的?

this is just a pointer to a current object of the class to which the function is a member of. this只是指向该函数所属的类的当前对象的指针。 Its more of a hidden parameter that is passed to every NON-STATIC method of a c++ class. 它更多是传递给c ++类的每个NON-STATIC方法的隐藏参数。 It simply points to a specific instance of a class along with all the data that the object has. 它只是指向类的特定实例以及对象拥有的所有数据。 So for your example: 因此,对于您的示例:

Token::~Token() {
if(this == nullptr) { return; }
.... }

This is just pointing to the object of the Token classes' destructor function. This只是指向Token类的析构函数的对象。

if(this == nullptr) { return; }

To be even more specific, the if statement above is seeing if the instance of the object is equal to a null reference. 更具体地说,上面的if语句查看对象的实例是否等于空引用。

Checking this with NULL in c++ is discouraged. 检查thisNULL在C ++是气馁。 this could be NULL when the method is invoked on a NULL pointer to the class. this当该方法是在一个NULL指针到类调用可以是NULL。 An example: 一个例子:

Token* token = nullptr;
token->~Token();

The code really should check if token is NULL in the first place, instead of checking NULL in the destructor. 该代码确实应该首先检查token是否为NULL,而不是在析构函数中检查NULL。

Token* token = nullptr;
if (token)
  token->~Token();

This link explains your question: http://www.viva64.com/en/b/0226/ 该链接说明了您的问题: http : //www.viva64.com/en/b/0226/

How Google and Foxit fixed this issue in pdfium: Google和Foxit如何在pdfium中解决此问题:

https://bugs.chromium.org/p/pdfium/issues/detail?id=4 https://groups.google.com/forum/#!topic/pdfium/8mTxtmle4ok https://bugs.chromium.org/p/pdfium/issues/detail?id=4 https://groups.google.com/forum/#!topic/pdfium/8mTxtmle4ok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM