14

I need to understand this pointer concept, preferably with an example.

I am new to C++, so please use simple language, so that I can understand it better.

sbi
  • 219,715
  • 46
  • 258
  • 445
Trupti
  • 597
  • 4
  • 8
  • 17
  • 3
    You need to ask a specific question about pointers. What is it that you don't understand, in particular ? – Paul R Dec 19 '10 at 15:39
  • what is the concept,the use of it..give simple example if possible – Trupti Dec 19 '10 at 15:41
  • 3
    @Paul R it is a specific question. It was worded poorly. – wheaties Dec 19 '10 at 15:50
  • 3
    I agree with @wheaties. Please __don't close__ this question. How on earth is this "not a real question"? Feel free to improve it, though. – sbi Dec 19 '10 at 16:04
  • 6
    When Paul Dirac made a rare error in an equation on the blackboard during a lecture one day, a couragous student raised his hand: "Professor Dirac," he declared, "I do not understand equation 2." When Dirac continued writing, the student, assuming that he had not been heard, raised his hand again and repeated his remark. Again Dirac merely continued writing... "Professor Dirac," another student finally interjected, "that man is asking a question." "Oh?" Dirac replied. "I thought he was making a statement." – SEngstrom Dec 19 '10 at 16:13

4 Answers4

9

this is a pointer to an instance of its class and available to all non-static member functions.

If you have declared a class, which has a private member foo and a method bar, foo is available to bar via this->foo but not to "outsiders" via instance->foo.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
6

The this pointer is used in a class to refer to itself. It's often handy when returning a reference to itself. Take a look at the proto-typical example using the assignment operator:

class Foo{
public:
    double bar;
    Foo& operator=(const Foo& rhs){
        bar = rhs.bar;
        return *this;
    }
};

Sometimes if things get confusing we might even say

this->bar = rhs.bar;

but it's normally considered overkill in that situation.

Next up, when we're constructing our object but a contained class needs a reference to our object to function:

class Foo{
public:
    Foo(const Bar& aBar) : mBar(aBar){}

    int bounded(){ return mBar.value < 0 ? 0 : mBar.value; }
private:

    const Bar& mBar;
};

class Bar{
public:

      Bar(int val) : mFoo(*this), value(val){}

      int getValue(){ return mFoo.bounded(); }

private:

      int value;
      Foo mFoo;
};

So this is used to pass our object to contained objects. Otherwise, without this how would be signify the class we were inside? There's no instance of the object in the class definition. It's a class, not an object.

wheaties
  • 35,646
  • 15
  • 94
  • 131
3

In object-oriented programming, if you invoke a method on some object, the this pointer points at the object you invoked the method on.

For example, if you have this class

class X {
public:
  X(int ii) : i(ii) {}
  void f();
private:
  int i;
  void g() {}
};

and an object x of it, and you invoke f() on x

x.f();

then within X::f(), this points to x:

void X::f()
{
  this->g();
  std::cout << this->i; // will print the value of x.i
}

Since accessing class members referred to by this is so common, you can omit the this->:

// the same as above
void X::f()
{
  g();
  std::cout << i;
}
sbi
  • 219,715
  • 46
  • 258
  • 445
1

A pointer is a variable type which points to another location in your program's memory. Pointers only can hold addresses of memory locations.

image

For example, if we say an "int pointer" - > it holds the memory address of a int variable.

"void pointer" -> can hold any type of memory address which is not a specific data type.

The & operator gives the address of a variable (or the value of the pointer where pointed).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sudantha
  • 15,684
  • 43
  • 105
  • 161
  • 4
    He's refering to the `this` pointer, not pointers in general. – wheaties Dec 19 '10 at 15:49
  • 1
    @wheaties: to be fair, this was not clear in the original question - it looked like a very vague and general "I don't understand pointers" cry for help as posted. – Paul R Dec 19 '10 at 18:04