与this指针混淆 [英] confused with the this pointer

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

问题描述

4:     typedef unsigned short  USHORT;
5:     #include <iostream.h>
6:
7:     class Counter
8:     {
9:     public:
10:       Counter();
11:       ~Counter(){}
12:       USHORT GetItsVal()const { return itsVal; }
13:       void SetItsVal(USHORT x) {itsVal = x; }
14:       const Counter& operator++ ();      // prefix
15:       const Counter operator++ (int); // postfix
16:
17:    private:
18:       USHORT itsVal;
19:    };
20:
21:    Counter::Counter():
22:    itsVal(0)
23:    {}
24:
25:    const Counter& Counter::operator++()
26:    {
27:       ++itsVal;
28:       return *this;
29:    }
30:
31:    const Counter Counter::operator++(int)
32:    {
33:       Counter temp(*this);
34:       ++itsVal;
35:       return temp;
36:    }
37:
38:    int main()
39:    {
40:       Counter i;
41:       cout << "The value of i is " << i.GetItsVal() << endl;
42:       i++;
43:       cout << "The value of i is " << i.GetItsVal() << endl;
44:       ++i;
45:       cout << "The value of i is " << i.GetItsVal() << endl;
46:       Counter a = ++i;
47:       cout << "The value of a: " << a.GetItsVal();
48:       cout << " and i: " << i.GetItsVal() << endl;
49:       a = i++;
50:       cout << "The value of a: " << a.GetItsVal();
51:       cout << " and i: " << i.GetItsVal() << endl;
52:     return 0;
53: }



为什么在这里使用this指针???为什么不能简单地像这样创建对象临时对象?



Why is the this pointer used here??? Why can''t just simply create the object temp like that?

33:       Counter temp;




###############

谢谢大家的帮助.但是我不得不承认,我仍然不了解它.这是我的一些问题和想法...




###############

Thanks for help all of you. But I have to admit that I still don''t understand about it. Here are some some questions and thoughts of me...

const Counter Counter::operator++(int)
    {

       ++itsVal;
       Counter temp(*this);
       return temp;
    }



1.为什么在此类方法中需要使用"int"自变量?
2.为什么使用此指针创建计数器温度?它看起来像是在构造函数或复制构造函数中使用的参数,但构造函数仅设置了它的Val ...实际上,this指针有点令人困惑.我只知道this指针具有要使用的对象的地址.
3. const表示返回的对象是常量吗?



编辑(将下面由答题者写的文本作为答案并在此处添加新问题)



1.Why is the argument of ''int'' needed in this class method?
2.Why is the this pointer used creating counter temp? It looks for me like a parameter used in constructor or copy constructor, but the constructor sets only itsVal... Actually, the this pointer is a bit confusing thing. I understand only that the this pointer has the address of object working with.
3.Does const mean that the returning object is constant?



Edit (Bringing text written below by the autor as answer and adding the new questions here)

推荐答案

通过传递this,临时计数器将使用的值进行初始化对象(itsVal)和函数将返回一个包含递增之前状态的副本.创建不带参数的temp会将itsVal设置为0(请参见Counter::Counter()实现).
By passing this, the temp counter is initialized with the values of the object (itsVal) and the function returns a copy containing the state before incrementing. Creating temp without an argument will set itsVal to 0 (see the Counter::Counter() implementation).


这是因为这是迭代器INT++,在这种情况下,您需要增加INT的值,但使用当前值.例如,下面的代码将首先输出1,然后输出2.
It is because this is the iterator INT++ In that case you want to increase the value of INT but use the current value. For example, the code below will first output 1 and then 2.
int a = 1;
cout << a++;
cout << a;


因此,迭代器需要在该点返回先前的值.

祝你好运!


Because of that the iterator needs to return the prior value at that point.

Good luck!


存储对另一个变量的引用的变量称为指针.指针被称为指向"它们所存储的引用的变量.

在这种情况下,星号(*)充当取消引用运算符,将其视为操作的上下文从指针到其指向的变量的移动很有用.

请考虑以下内容:
A variable which stores a reference to another variable is called a pointer. Pointers are said to "point to" the variable whose reference they store.

In this case the asterisk (*), acts as dereference operator and it''s useful to think of it as moving context of the operation from the pointer to the variable it points to.

Consider the following:
this->itsVal++; 
(*this).itsVal++; // dereference
Counter* ptr = this; // pointer declaration of ptr and assignment of this to ptr
ptr->itsVal++; 
(*ptr).itsVal++; // dereference



最好的问候
Espen Harlinn



Best regards
Espen Harlinn


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

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