简体   繁体   English

您如何在C ++中的对象中使用“ this”指针?

[英]How do you use the “this” pointer in C++ in objects?

This is my class, 这是我的课

#ifndef CARD_H
#define CARD_H

class Card
{
    private:
            int suit, value;
    public:
            Card (int, int);
            int Compare (Card)const;
            void print_card()const;
};

#endif

For my compare function, I want to compare the values, and then suits, of two cards and then return 1 if the object card is greater and 0 if the card i'm comparing it to is greater. 对于我的比较功能,我想比较两张卡的值,然后进行比较,如果目标卡更大,则返回1,如果我比较的卡更大,则返回0。 How do I accomplish this? 我该如何完成?

And my Card constructor is supposed to create a new card, but im not allowed to use "new" in the constructor statement. 我的Card构造函数应该创建一个新的卡片,但是我不能在构造函数语句中使用“ new”。 How am I supposed to create new cards if this is the case? 如果是这种情况,我应该如何创建新卡?

This is what i tried for compare: 这是我尝试比较的:

int Card::Compare(Card c1) const
{

    if ( c1 == NULL && this == NULL ) return 0;
    else if ( c1 == NULL ) return -1;
    else if ( this == NULL ) return 1; 

    else if ( c1.value > this.value ) return 1;
    else if ( c1.value < this.value ) return -1;
    else if ( c1.value == this.value )
    {
        if ( c1.suit > this.suit ) return 1;
        if ( c1.suit < this.suit ) return -1;
    }

I think you should read a basic C++ tutorial. 我认为您应该阅读基本的C ++教程。 In short: 简而言之:

1) You don't need the this pointer, it is implicit and it is not a good practice to use it to access member in general (sometimes you don't have choice to disambiguate with a local variable): 1)您不需要this指针,它是隐式的,一般来说,使用它来访问成员不是一种好习惯(有时您没有选择与局部变量进行歧义的选择):

bool Card::Compare(const Card &c1) const  // Note, usage of const reference
{ 
    // c1 is NOT a pointer, no need to check for NULL
    // checking this is not a usual idiom

    if ( c1.value > value ) return true;  // same as if ( c1.value > this->value ) return true;
    if ( c1.value < value ) return false; 
    if ( c1.suit > suit ) return true;
    if ( c1.suit < suit ) return false;
    return true;
}

2) The constructor is called when the client create a Card, it does not have to do anything. 2)当客户端创建Card时,将调用构造函数,它无需执行任何操作。

Card *some_card = new Card(1, 2); // constructor is called with 1 and 2

Note that new is not required for automatic allocation: 请注意,自动分配不需要new

Card some_card(1, 2); // constructor is called with 1 and 2

Now, multiple things could be improved in your code, like not using int as it is not type safe, and using operators > and < to compare card. 现在,可以在代码中进行多项改进,例如,不使用int类型不安全的int以及使用运算符>和<比较卡。

In C++ the this pointer is a pointer to the present instance of the class. 在C ++中, this 指针是指向该类当前实例的指针。

Because it is a pointer you must treat it as a pointer: 因为它是一个指针,所以必须将其视为指针:
this->member or (*this).member this->member(*this).member

In C++ you only need to use the this pointer to avoid naming conflicts with method parameters. 在C ++中,只需要使用this指针即可避免与方法参数的命名冲突。

I have never used the this pointer because I refer to the members directly and purposely use different names between method parameters and member variables. 我从未使用过this指针,因为我直接引用了成员,并且故意在方法参数和成员变量之间使用不同的名称。

You usually use the "this" object when you want to use it outside of your class. 当您想在课堂外使用“ this”对象时,通常会使用它。 If you are calling a function that is outside of your class and want to pass the current object to that function, you will pass in the "this" pointer. 如果您要调用类之外的函数,并且希望将当前对象传递给该函数,则将传递“ this”指针。 The function obviously has to take the same class (or a derrived class) where you want to call the function from. 该函数显然必须采用您要从中调用该函数的相同类(或派生类)。

Example: 例:

void DoSomeThingElse(A *object) {};

class A
{
    void DoSomething()
    {
        DoSomethingElse(this);
    }
}  

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

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