473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

member functions and implicit "this"

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 ?
--
http://arnuld.blogspot.com

Aug 20 '07 #1
3 2488
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

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Aug 20 '07 #2
On Aug 20, 11:35 am, arnuld <geek.arn...@gmail.comwrote:
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

Aug 20 '07 #3
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
Aug 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
2
by: danny.dion | last post by:
Hi ! I have a question about JScript : I have an object class wich dynamically creates a control in the page. Then it binds an event to that control, pointing on one of its methods (the...
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
14
by: Alexander Dong Back Kim | last post by:
Dear all, I used to use C++ programming language at all time but moved to C# and Java. Few days ago, I restarted studying about C++ with a very beginner's mind. I wrote a simple class and gcc...
8
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
2
by: unauthorized | last post by:
The short story: I need to be able to cast a function pointer for any function and class to an intermediate type, so I could call it from any point of my program using the "__asm call" instruction....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.