1

So far to my understanding, when defining a pointer variable, we are allocating space in RAM for that variable.

int *p;  

Would define a space in RAM. Then we assign a memory address to that pointer using `&variable'.

I'm looking over at an example on: *this vs this in C++ The code is:

#include <iostream>

class Foo
{
    public:
        Foo()
        {
            this->value = 0;
        }

        Foo get_copy()
        {
            return *this;
        }

        Foo& get_copy_as_reference()
        {
            return *this;
        }

        Foo* get_pointer()
        {
            return this;
        }

        void increment()
        {
            this->value++;
        }

        void print_value()
        {
            std::cout << this->value << std::endl;
        }

    private:
        int value;
};

int main()
{
    Foo foo;
    foo.increment();
    foo.print_value();

    foo.get_copy().increment();
    foo.print_value();

    foo.get_copy_as_reference().increment();
    foo.print_value();

    foo.get_pointer()->increment();
    foo.print_value();

    return 0;
}

I don't understand what the purpose of putting the * operator in front Foo* get_copy() and Foo* get_pointer() does. Why do I get an error if I removed the * from the Foo* functions while returning this not *this?

edit:

Also, why is:

foo.get_copy().increment();
foo.print_value();

yielding 1 not 2?

Community
  • 1
  • 1
eveo
  • 2,797
  • 15
  • 61
  • 95
  • 1
    the return type for `get_copy` is wrong. It should return by value, not pointer - `Foo get_copy()` – Captain Obvlious Apr 16 '13 at 22:44
  • Foo get_copy yields an error unless `this` is changed to `*this`. – eveo Apr 16 '13 at 23:03
  • `*this` is exactly what it's supposed to be. Look at the original source. You copied it wrong. – Captain Obvlious Apr 16 '13 at 23:07
  • Oh whoops, I copied my modified version. Edited. So `this` is a pointer by nature, though deferenced. A pointer can only be deferenced if it has a pointee, and that pointee is the current object. When we dereference a pointer, we are changing the state of the current object being pointed to, aka the pointee. Am I understanding this right? – eveo Apr 16 '13 at 23:10
  • Ok, my turn to confuse. A pointer variable should only be dereferenced if it contains a valid pointer value. A valid pointer value points to a living object. When you dereference a pointer you do not change the objects state **until** you call a mutating member function or directly modify a member variable. Generally speaking ;) – Captain Obvlious Apr 16 '13 at 23:27
  • You are not changing any objects by using `*`. See my answer below for the 2 meanings of `*`. – Mikkel Apr 16 '13 at 23:27

5 Answers5

2

I don't understand what the purpose of putting the * operator in front Foo* get_copy() and Foo* get_pointer() does

Foo* get_pointer()

Foo* is a pointer that points to Foo object.

this is also a pointer that implicitly bind to the calling object of the member function. That's why the return type of those functions are Foo* not Foo.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
taocp
  • 23,276
  • 10
  • 49
  • 62
1

this, is always a pointer in C++, though you don't mention it explicitly anywhere. So while returning a this pointer, should use Foo*

this is actually an implicit object passed during function call, which is a pointer to the object which calls the function

Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
  • @eveo `this` isn't a variable. It's an expression, and is an rvalue, so it doesn't have an address (and it cannot be modified). The _type_ of the expression `this` is a pointer type. – James Kanze Apr 16 '13 at 22:44
1

The * is part of the type. So int means type int, and int* type pointer to int. If the function returns pointer to int, it is int* foo(), and if it retu rns a pointer to Foo, it is Foo* Foo::get_pointer().

The definition reserves space for the defined object. A declaration doesn't reserve any space, and definitions of things that aren't objects (e.g. references or functions) don't reserve any space either, at least not that you can see. (Obviously, a function does exist somewhere in memory, and in many cases, the compiler will need space as well for its implementation of a reference. But they are invisible within the scope of C++.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

It seems that you have changed the code from the example that you refer to so that get_copy() no longer returns a copy.

There are 2 ways of using * in your code example. One is for type declaration and the other is the dereferencing operator.

First the type declarations:

int *p means declaring p as a variable of type "pointer to an int".

Foo *get_pointer() means that the function get_pointer will return a value of type "pointer to a Foo object".

Now the dereferencing:

*p means "the value that p points to".

int a = 42; 
int *p;  // p is of type "pointer to an int"
p = &a;  // set p to the address of a (p now "points to" a)

a = 117;  // change the value of a
int x = *p; // set x to the value that p points to (which is a) - x will be 117

this is just a pointer to the object. *this means "the object that this points to". In your example this is of type Foo* (pointer to a Foo object) while *this is of type Foo (a Foo object).

Mikkel
  • 3,284
  • 2
  • 18
  • 20
-1

"this" is a pointer.
you want to return a pointer to the instance (a specific allocated object).

 Foo* get_pointer(){
            return this;
        }

or you want to return a pointer to the copy, allocate a new object.

//need to implement the copy here
       Foo* get_copy(){
            return this;
        }

and not to the reference (address of the instance). this is why you need to return the pointer.

Gilad
  • 6,437
  • 14
  • 61
  • 119
  • You are not returning a pointer to a copy you are returning a pointer to the current object. No copy has been made. It should be `Foo get_copy() { return *this; }` if you want to return a copy or instantiate one in dynamic storage and return a pointer to it. – Captain Obvlious Apr 16 '13 at 22:50
  • @CaptainObvlious you are right, I meant that you need yo implement the copy. – Gilad Apr 16 '13 at 22:53