member functions and implicit "this"

A

arnuld

this is from C++ Primer 4/e page 260:

there is a class named Sales_item and same_isbn is a member function of
that class.

bool same_isbn(const sales_item &rhs) const {
return isbn == rhs.isbn;
}

author says the word const modifies the type of implicit "this" paramater.
when we will call this:

total.same_isbn(trans)

then the implicit this parameter will be a --const Sales_item*-- that
points to total and it is as if the body of same_isbn is written as:

bool Sales_item::same_isbn (const Sales_item *const this,
const Sales_item &rhs) const
{
return (this->isbn == rhs.isbn);
}


i don't get it, in the paragraph above the author says const modifies the
type of implicit this to --const Sales_item*-- and in the code he says it
is: --const Sales_item *const this-- (notice the 2 consts)


what exactly the type of this is now ?
 
N

Neelesh Bodas

this is from C++ Primer 4/e page 260:

there is a class named Sales_item and same_isbn is a member function of
that class.

bool same_isbn(const sales_item &rhs) const {
return isbn == rhs.isbn;

}

author says the word const modifies the type of implicit "this" paramater.
when we will call this:

total.same_isbn(trans)

then the implicit this parameter will be a --const Sales_item*-- that
points to total and it is as if the body of same_isbn is written as:

bool Sales_item::same_isbn (const Sales_item *const this,
const Sales_item &rhs) const
{
return (this->isbn == rhs.isbn);

}

i don't get it, in the paragraph above the author says const modifies the
type of implicit this to --const Sales_item*-- and in the code he says it
is: --const Sales_item *const this-- (notice the 2 consts)
what exactly the type of this is now ?

'this' is always a "constant pointer". Thus, for a class X, type of
"this" is "X* const". Observe that you can'd do something like this:

class X
{
void doo()
{
this = new X(); //Error, non-lvalue in assignment
}
};


For a const object, the type of "this" is "a const pointer to a const"
i.e "const X* const".

-N
 
D

Daniel Kraft

there is a class named Sales_item and same_isbn is a member function of
that class.

bool same_isbn(const sales_item &rhs) const {
return isbn == rhs.isbn;
}

author says the word const modifies the type of implicit "this" paramater.
when we will call this:

total.same_isbn(trans)

then the implicit this parameter will be a --const Sales_item*-- that
points to total and it is as if the body of same_isbn is written as:

bool Sales_item::same_isbn (const Sales_item *const this,
const Sales_item &rhs) const
{
return (this->isbn == rhs.isbn);
}


i don't get it, in the paragraph above the author says const modifies the
type of implicit this to --const Sales_item*-- and in the code he says it
is: --const Sales_item *const this-- (notice the 2 consts)

The first const makes the object pointed to const (that's what the const
on the method changes), and the second const makes the pointer itself
const, so you can't do something like

this=new Sales_item();

I think this is a general rule for this pointer, but I'm not absolutely
sure.

This const would be there even for a non-const method.

Cheers,
Daniel
 
S

Scofield

arnuld 写é“:
this is from C++ Primer 4/e page 260:

there is a class named Sales_item and same_isbn is a member function of
that class.

bool same_isbn(const sales_item &rhs) const {
return isbn == rhs.isbn;
}

author says the word const modifies the type of implicit "this" paramater.
when we will call this:

total.same_isbn(trans)

then the implicit this parameter will be a --const Sales_item*-- that
points to total and it is as if the body of same_isbn is written as:

bool Sales_item::same_isbn (const Sales_item *const this,
const Sales_item &rhs) const
{
return (this->isbn == rhs.isbn);
}


i don't get it, in the paragraph above the author says const modifies the
type of implicit this to --const Sales_item*-- and in the code he says it
is: --const Sales_item *const this-- (notice the 2 consts)


what exactly the type of this is now ?
in a non-const member function, "this" is defined as Class_name * const;
in a const member function, this is defined as const Class_name * const
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top