在C++中常有this这个指针,然而至含今我对其还是一知半解,它到底是在什么时个构造,什么时候被析构,具体代表那些东西,还有*this.希望各们朋友能畅所欲言!!

解决方案 »

  1.   

    this就是代表当前对象。指向自己本身,并没有什么构造不构造的说法。
      

  2.   

    this最终是作为一个隐含参数传递给编译器生成的函数
      

  3.   

    The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
    (come from MSDN)
      

  4.   

    this
    C++ Specific —>The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function callmyDate.setMonth( 3 );
    can be interpreted this way:setMonth( &myDate, 3 );
    The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.The expression (*this) is commonly used to return the current object from a member function.Note   Modifying the this pointer is illegal in the latest version of C++.END C++ SpecificExample// Example of the this pointer
    void Date::setMonth( int mn )
    {
       month = mn;            // These three statements
       this->month = mn;      //     are equivalent
       (*this).month = mn;
    }
      

  5.   

    this 就是一个隐含参数,他指的就是对象本身,《深入浅出mfc》好像有详细讲解,只是书在家,不能帮你查了
      

  6.   

    我也知道他是指的本身,可我还想知道的祥细一点,不然也不会给150分。比如说,有人为什么会说,一般不要在程序的构造数据中使有this指针,也有人说:只要小心也可在构造函数中this指针。《深入浅出mfc》确实有这么一点,但不是详细讲解,加代码都不足30行。我想从一个比较深层去了解这处this指针,打个比方来说吧,它是在什么时候产生的?就说一个类吧,程序编译时,它是先把成员变量编译的,那是不是这个时候也顺便产生了this呢?
    那我又想问,要是我在类的一个成员函数中执行“delete this”结果又会怎样呢,要是我又在析构函数中delete this,结果是不是一样呢?
    等等,大家能否讨论下呢?
      

  7.   

    平时我们 char* p = new char[10];这P是我们自己定义的,往后不用了,我们就delete p; p = NULL,也就没事了,可是这个 “白送”this的看不到摸不着,平是要用的话总有点不放心。还有他能在类中使用,而在不同类中有不同(指本身,不同类本身本该不同),那是否说它就是个局部指针了,即然是局部的它又是在什么时候指向他本身呢即是在什么时候初始化的呢?即然能指向本身那说明在指的那时,他(类)本身应该是完整的(构造好了,最起码有一部分是构造好了),那这个时刻到低又是在什么之前,什么之后呢?能否说清呢,还有this 到低是 void* 呢还是其它类型呢?
    在程序执行过程中是否能给 this 赋个值呢?要是行的话,一个本该指向本身的指针一下子指向别人了,结果又如何呢?
      

  8.   

    thisz指针应该是在对象生成的时候产生的,他指向对象本身,类中的默认构造函数就对this 指针进行了初始化,在对象被析构之后,this指针也就清除了,在c++primer中有对this指针比较详细地介绍,对于什么时候可以用this指针也有说明,可以去查一下。
      

  9.   

    我觉得可以这么认为:this指针是在构造函数之前产生的。
    产生类的一个对象时,系统会先为这个对象声请内存,然后调用构造函数对这个对象的数据区部分进行初始化。在构造函数被调用之前,对象应该已经存在,而this指针是在对象存在之后构造函数之前被定义,所以在构造函数中可以使用this指针,又因为对象未被初始化(构造函数未执行完),this指针的成员值是不确定的,所以此时访问this指针的成员的值或调用成员函数是危险的。
    例如:
    class A;
    class B;class A
    {
    public:
        B* pB;
        int* pAny;
        void Do1(B* p)
        {
            if (p->pAny != NULL) delete p->pAny;
        }
        void Do2(B* p)
        {
            pB = p;
        }
    };A a;class B
    {
    public:
        int* pAny;
        void Empty()
        {
            if (pAny != NULL) delete pAny;
        }public:
        B()
        {
            a.pB = this; // 这是安全的
            a.Do1(this); // 不安全
            a.Do2(this); // 这是安全的
            a.pAny = this->pAny; // 这是安全的
            *a.pAny = *this->pAny; // 不合理,*this->pAny的值是不确定的
            this->Empty(); // 非法的,此时pAny并未申请内存,不应该调用delete pAny;
            this->pAny = NULL; // 这是安全的
            this->pAny = new int; // 这是安全的
        }
    };总的来说,在构造函数中使用this指针有3种情况发生:
    1、是安全的
    2、是不合理的
    3、是非法的
      

  10.   

    类似的,this指针是在析构函数之后清除的。
      

  11.   

    楼上说的意思差不多了。
    类中的静态变量不含this指针,是所有类对象公用的。
      

  12.   

    只要理解指针是一个地址,程序中调用this的地方,在编译的时候被填充以对象实例的地址,下面随便你怎么理解了。