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++ > BOOK: Ivor Horton's Beginning Visual C++ 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Visual C++ 2005
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2005 by Ivor Horton; ISBN: 9780764571978
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning 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
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 5th, 2007, 03:43 PM
Authorized User
Points: 174, Level: 3
Points: 174, Level: 3 Points: 174, Level: 3 Points: 174, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2006
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch7 Const Member Functions of a Class

Hello all,

In Beginning Visual C++, Ivor Horton says (on page 361, chapter 7) if we declare an object as
'const', it will have a 'this' pointer that is const, so the compiler
will not allow any class member function to be called that does not
assume the 'this' pointer is const.

In the example below, I haved added 'const' to the 'CBox match' object
in the main function as Ivor suggests in the example, and this did not give me the error I was
expecting from his example. The book says I will get an error at the
class function 'Compare()' unless I make that class function const as
well. But I get the opposite results. Everything is fine if I don't
make the class member function 'Compare' const. When I do make it
const, I get an error at class function 'Volume' (which I sort of
expected - it would need to accept a const pointer too, right?).

Anyhoo, what gives? Why can I declare the object const, but not the
class function that the object later calls from main() and the program still runs (but Ivor's didn't)?? Is it
because the class function 'Compare' doesn't actually modify member
data? If so, any speculation as to why the author got the compiler
error at 'Compare' instead of 'Volume'??

Thanks in advance,

[code]
// Ex7_07.cpp
// Calculating the volume of a box with a member function
#include <iostream>
using std::cout;
using std::endl;

class CBox // Class definition at global
scope
{
public:

    // Constructor definition
    CBox(double lv=1, double bv=1, double hv=1)
    {
        cout << endl << "Constructor Called.";
        m_Length = lv;
        m_Width = bv;
        m_Height = hv;
    }
    // Function to calculate the volume of a box
    double Volume()
    {
    return m_Length*m_Width*m_Height;
    }

    // Function to compare two boxes which returns true (1)
    // if the first is greater than the second, and false (0) if
otherwise
    int Compare(CBox xBox)
    {
      return this->Volume() > xBox.Volume();
    }
private:
    double m_Length; // Length of box in inches
    double m_Width; // Width of box in inches
    double m_Height; // Height of box in inches

friend double BoxSurface(CBox aBox);
};

double BoxSurface(CBox aBox)
{
  return 2.0*(aBox.m_Length*aBox.m_Width) +
             (aBox.m_Length*aBox.m_Height) +
             (aBox.m_Height*aBox.m_Width);
}

int main()
{
  const CBox match(2.2, 1.1, 0.5);
  CBox cigar(8.0,5.0,1.0);

  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;
}



Sincerely,
Brian
__________________
Sincerely,
Brian
  #2 (permalink)  
Old March 5th, 2007, 04:01 PM
Authorized User
Points: 174, Level: 3
Points: 174, Level: 3 Points: 174, Level: 3 Points: 174, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2006
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, nevermind, I'm an idiot ;)

I had set the wrong object to const. Below is the correct declaration from main()
Code:
  
CBox match(2.2, 1.1, 0.5);
const CBox cigar(8.0,5.0,1.0);
Sincerely,
Brian
 


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
Page 361 Constant Member Functions Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 June 9th, 2006 08:01 AM
Arraylist::Sort() by class member jilly Visual C++ 2005 0 March 22nd, 2006 05:49 PM
How to Assign a Desc. to a Class Member BrianWren VB.NET 2002/2003 Basics 3 June 1st, 2005 02:07 PM
Invoking event from member variable class adxabuan Visual C++ 0 February 2nd, 2004 05:04 AM
Forum class (member) explination?? drfunkie BOOK: ASP.NET Website Programming Problem-Design-Solution 0 June 15th, 2003 07:46 PM



All times are GMT -4. The time now is 07:14 AM.


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