user3380850
user3380850

Reputation: 11

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. 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. 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;
    }

Upvotes: 0

Views: 3582

Answers (3)

Joky
Joky

Reputation: 1628

I think you should read a basic C++ tutorial. 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):

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.

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

Note that new is not required for automatic allocation:

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.

Upvotes: 0

tsragravorogh
tsragravorogh

Reputation: 3153

You usually use the "this" object when you want to use it outside of your class. 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. 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);
    }
}  

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57728

In C++ the this pointer is a pointer to the present instance of the class.

Because it is a pointer you must treat it as a pointer:
this->member or (*this).member

In C++ you only need to use the this pointer to avoid naming conflicts with method parameters.

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.

Upvotes: 1

Related Questions