Question about "this-> pointers"

This is a discussion on Question about "this-> pointers" within the C++ Programming forums, part of the General Programming Boards category; I am having trouble understanding a functon that uses a ->this pointer. I also know that the function may work ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Ft Lauderdale, FL USA
    Posts
    7

    Question about "this-> pointers"

    I am having trouble understanding a functon 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.

    Here's the code:

    Code:
    #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;
    }
    The 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?? shouldn't it need an if (xxx.volume() > xxx.volume())
    return 1;
    else
    return 0;

    ??

    It looks a little "magical" to me. Any help would be really appreciated. Thanks!
    Last edited by leogoldseed; 10-13-2008 at 11:17 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    The 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?? shouldn't it need an if (xxx.volume() > xxx.volume())
    return 1;
    else
    return 0;

    ??
    The result of this->Volume() > xBox.Volume() is a bool. A bool, when converted into an integer, becomes 1 if it is true, and 0 if it is false. Hence, you do not need that if statement. However, perhaps you actually want to declare Compare() as returning a bool instead of an int.

    By the way, you need to improve your indentation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Ft Lauderdale, FL USA
    Posts
    7
    Thanks a lot laserlight! This is the kind of quick and small explanation that goes a long way in books, but is often skipped by experienced programmers who write the programming books. In yes, you a right, I need to improve my indentation! Thanks for that too!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    You're welcome

    Incidentally, have you learnt about const-correctness and passing by reference yet? If so, note that Volume() and Compare() should be const member functions, and that Compare() should take another CBox by const reference.

    Also, the member variables should be initialised in the constructor initialisation list, though in this case assigning to them in the constructor body as you have done is not a problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Ft Lauderdale, FL USA
    Posts
    7
    I am learning classes right now, and I see that the chapter has info about const-correctness and passing by reference later in the book.

    Quick questions though, (I actually took this code from my c++ textbook) why would the author use int instead of bool in the book? Does that serve any specific purpose?

    So the expression using the boolean variable: if(cigar.Compare(match) == true) is the same as (cigar.Compare(match)) using the int?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Quick questions though, (I actually took this code from my c++ textbook) why would the author use int instead of bool in the book? Does that serve any specific purpose?
    It could be an oversight. When I see a comparison function that returns an int, it is usually to follow the strcmp() idiom: if the number returned is less than/equal to/greater than 0, then the first object is less than/equal to/greater than the second object, respectively. However, in this case Compare() merely compares for "greater than", so implementing it by overloading operator> would be better, in my opinion.

    So the expression using the boolean variable: if(cigar.Compare(match) == true) is the same as (cigar.Compare(match)) using the int?
    Yes, though if (x == true) is generally the same as if (x). Perhaps you meant if (cigar.Compare(match) == 1).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Ft Lauderdale, FL USA
    Posts
    7
    I did not realize that: if (x == true) is generally the same as if (x) !! Now it makes all the sense of the world. That's why he typed it: if(cigar.Compare(match)).

    You've been of enormous help. Thanks SO much again!
    Last edited by leogoldseed; 10-14-2008 at 12:23 AM.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    For someone who is already used to the fact that "if (x == true)" is the same as just "if (x)"; "if (x == true)" looks like how "if ((x == true) == true)" would look to someone who hasn't yet caught onto it, if that helps put it into perspective!

    Of course it makes more sense if instead of 'x', your bool is named something like 'itIsRaining'.

    (Makes for even nicer reading if you're using a Pascal-like language that has a 'then' at the end of the 'if')
    E.g. "if it_is_raining then use_your_umbrella;"
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about -> dereferencing
    By newlearner in forum C Programming
    Replies: 10
    Last Post: 06-22-2009, 12:39 AM
  2. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM
  3. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 06:24 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21