Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++ 2005
Visual C++ 2005 For discussion of Visual C++ 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 14th, 2008, 12:12 AM
Registered User
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default this-> pointers - (Page 358) Ivor Horton's book

I am having trouble understanding a function that uses a ->this pointer. I also know that the function may work without using a ->this pointer, but the point is that I don't understand how the compiler understands it, or how it actually works, after reading it.
(Page 358) Ivor Horton's book:

// Ex7_10.cpp
// Using the pointer this

#include <iostream>
using std::cout;
using std::endl;

class CBox // Class definition at global scope
CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0) // Constructor definition
    cout << endl << "Constructor called.";
    m_Length = lv; // Set values of
    m_Width = bv; // data members
    m_Height = hv;

double Volume() // Function to calculate the volume of a box
    return m_Length*m_Width*m_Height;

int Compare(CBox xBox) // Function to compare two boxes which returns true (1) if the first is greater than the second, and false (0) otherwise
    return this->Volume() > xBox.Volume();

    double m_Length; // Length of a box in inches
    double m_Width; // Width of a box in inches
    double m_Height; // Height of a box in inches

int main()
CBox match(2.2, 1.1, 0.5); // Declare match box
CBox cigar(8.0, 5.0,1.0); // Declare cigar box

cout << endl << "match is smaller than cigar";
cout << endl << "match is equal to or larger than cigar";
cout << endl;

return 0;

So as you can see, this compare() function that was defined in the class definition returns 1 if the statement -this->Volume() > xBox.Volume()- is true, and 0 if not.

BUT WHY?? I don't get it. WHo decided is going to return a 1 or a 0? shouldn't it need an if (xxx.volume() > xxx.volume())
return 1;
return 0;


I really don't get it, and I don't want to just disregard it, because it still looks a little "magical" And I can't find any explanation as to why it's capable of doing this in the book.

Any help would be really appreciated. Thanks in advance!

Old January 30th, 2009, 04:19 AM
Registered User
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile @

this pointer points to object who is invoking a member function of a class,thus *this gives the object itself

puneet vyas
Old September 7th, 2010, 04:26 AM
Authorized User
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post

look at this example:
//somewhere in the code

int a = 100, b = 99;
bool result;
result = a > b;
The above code initializes result to 1. Exactly,the same happens in your Compare function's return statement.
Remember that every comparison in your code is replaced by true or false (boolean values).After the expression this->Volume() > xBox.Volume() evaluated, the value true or false sits instead of the expression.And then the true/false is implicitly converted to an integer corresponding true/false (that is 1/0) when returned by the function.
So your function's return statement changes to one of these :
return 1;
return 0;
Hope this short post helped;

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ivor Horton's Beginning C++ Firedrill C++ Programming 18 December 2nd, 2008 06:32 PM
Page 40 missing Ivor Horton's Beginning Visual C++ margoskafu Visual C++ 2005 0 December 12th, 2007 11:38 PM
Ivor Horton's book lshafer Visual C++ 2005 0 October 15th, 2007 08:07 AM
Errata for Ivor Horton's book ewnowak All Other Wrox Books 1 April 10th, 2006 02:34 PM
About Ivor Horton's book 357mag Visual C++ 1 June 22nd, 2005 07:05 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.