Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++ 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 14th, 2008, 12:12 AM
Registered User
 
Join Date: Oct 2008
Location: , , .
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
{
public:
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();
    }

private:
    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

if(cigar.Compare(match))
cout << endl << "match is smaller than cigar";
else
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;
else
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!

Reply With Quote
  #2 (permalink)  
Old January 30th, 2009, 03: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








regards
puneet vyas
Reply With Quote
  #3 (permalink)  
Old September 7th, 2010, 04:26 AM
Authorized User
Points: 100, Level: 1
Points: 100, Level: 1 Points: 100, Level: 1 Points: 100, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Post

look at this example:
Code:
//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 :
Code:
return 1;
or
Code:
return 0;
Hope this short post helped;
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ivor Horton's Beginning C++ Firedrill C++ Programming 18 December 2nd, 2008 05:32 PM
Page 40 missing Ivor Horton's Beginning Visual C++ margoskafu Visual C++ 2005 0 December 12th, 2007 10: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



All times are GMT -4. The time now is 02:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.