0

if we create a class like this :

class Sales_data
{
  std::string isbn() const {return bookNo;}
  std::string bookNo;
};

And we make a object total;

Sales_data total;
total.isbn();

The C++ Primer, fifth edition, says (page 258),"when we call a member function, this is initialized with the address of the object on which the function was invoked " ,it like this:

Sales_data::isbn(&total)

and the book also write,we can get the bookNo like :

std::string isbn()const {return this->bookNo;}

I think the implicit parameter "this" just like a pointer, but i can't see it type,would anybody help me point what wrong i think and what should i do to understand the implicit parameter 'this' and this parameter works for?

@Jason C my extra question: this is a pointer,so it behave like a normal pointer,

#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
  int a = 1;
  int * b = &a;
  cout << "the b is " << b << endl;
  cout << "the &a is " << &a << endl;
  cout << "the *b is " << *b << endl;
  cout << "the &b is" << &b << endl;
  return 0;
}

on my computer the output is :

the b is 0110FCEC
the &a is 0110FCEC
the *b is 1
the &b is0110FCE0

then ,What's the use of the type of the pointer.

Bee
  • 41
  • 1
  • 6
  • Maybe this [anwser](http://stackoverflow.com/questions/6067244/type-of-this-pointer) helps. – Marcin Nov 14 '14 at 03:00
  • In your case, it is either a `Sales_data *` or `const Sales_data *` depending on the context. Inside `isbn()`, it is the latter. – Jason C Nov 14 '14 at 03:03
  • This isn't just like a pointer, it is a pointer. – BlamKiwi Nov 14 '14 at 03:03
  • This question has nothing to do with [tag:primes], and "I can't see it type that can be store the address of object" is meaningless. Please restate your question in standard English. – user207421 Nov 14 '14 at 03:08
  • sorry about that,i already delete it and i will explain my question @Jason C – Bee Nov 14 '14 at 05:30

3 Answers3

3

this is not a parameter, it is a way for an object to refer to itself.

If you use visual studio or any modern IDE you can check that this has the same type as the class of which it is a member of.

There is a good book called "The C++ Object Model" by Stanley B. Lippman which can help understand.

ginger
  • 83
  • 2
2

Even if not defined as such in the standard, every implementation I am aware of makes this an implicit parameter to a member function and can be viewed as such.

In C++, you do

 object->function () ;

In contrast, in Ada the syntax is

function (object) ;

The object is then an explicit parameter to the member function. The this variable is a product of C++'s member calling syntax. Instead of the programmer having to explicitly declare a parameter identifying the object (as in Ada), C++ does this automatically for you (this).

In most implementations, C++ parameters are bound to offsets to locations on the stack or to registers. This is implemented in the very same way as other parameters (either bound to a stack offset or a register).

user3344003
  • 20,574
  • 3
  • 26
  • 62
0

this is a pointer to whatever instance of an object the member function is being called on (note that there is no this in static member functions or non-member functions, then).

In your case, it is either a Sales_data * or const Sales_data * depending on the context. Inside isbn(), it is the latter.

This (contrived) example illustrates its value:

class Example {
public:
    void function (Example *x);
};

void Example::function (Example *x) {
    if (x == this)
        cout << "x is this!" << endl;
    else
        cout << "x is not this." << endl;
}

Now if we do:

Example a;
Example *b = new Example();

a.function(&a);  // outputs "x is this!"
b->function(b);  // outputs "x is this!"

a.function(b);   // outputs "x is not this!"
b->function(&a); // outputs "x is not this!"

Also, since it's a pointer to the "current" instance of the object:

class Example2 {
public:
    int k;
    void function ();
};

void Example2::function () {
    k = 42; 
    this->k = 42; // does the same thing as above!
}
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • actually,in my primary question ""I can't see it type that can be store the address of object" is that i want known where the this parameter store the object address,i try some simple but haven't figure out and i write it on my question. – Bee Nov 14 '14 at 05:35