Using a const when returning a reference to an object

Discussion in 'Programmer's Corner' started by mentaaal, Jan 14, 2009.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys, I'm practising using the this pointer and trying to make a member function const at the same time.

    header file:
    Code ( (Unknown Language)):
    1. #ifndef rectangle_h
    2. #define rectangle_h
    3.  
    4. class rectangle
    5. {
    6. public:
    7.     rectangle(double = 0,double = 0, double = 0);
    8.     rectangle & setlength();
    9.     rectangle & setwidth();
    10.     rectangle & getlength();
    11.     rectangle & getwidth();
    12.     rectangle & calculateArea();
    13.     rectangle & displayArea();
    14. private:
    15.     double area, length, width;
    16. };
    17.  
    18. #endif
    19.  
    definition file:
    Code ( (Unknown Language)):
    1. #include "Rectangle.h"
    2. #include <iostream>
    3. using namespace std;
    4.  
    5.  
    6. rectangle::rectangle(double Area, double Length, double Width)
    7. {
    8.     area = Area;
    9.     length = Length;
    10.     width = Width;
    11.  
    12. }
    13.  
    14. rectangle &rectangle::calculateArea()
    15. {
    16.     area = length*width;
    17.     return *this;
    18. }
    19.  
    20. rectangle &rectangle::displayArea()
    21. {
    22.     cout << "The area of rectangle with length " << length << " and width " << width << " is " << area << endl;
    23.     return *this;
    24. }
    25.  
    26. rectangle &rectangle::getlength()
    27. {
    28.     cout << "The length of the rectangle is " << length << endl;
    29.     return *this;
    30. }
    31. rectangle &rectangle::getwidth()
    32. {
    33.     cout << "The width of the rectangle is " << width << endl;
    34.     return *this;
    35. }
    36.  
    test file:
    Code ( (Unknown Language)):
    1. #include "Rectangle.h"
    2. #include <iostream>
    3. using namespace std;
    4.  
    5. int main()
    6. {
    7.     rectangle rectangle1(1,1,1); //create object with area, width and length of 1
    8.     rectangle1.getlength().getwidth().displayArea();
    9.    
    10.     rectangle rectangle2;
    11.     rectangle2.getlength().getwidth().displayArea();
    12.     rectangle2 = rectangle1;
    13.     cout << "After using a copy contructor\n";
    14.     rectangle2.getlength().getwidth().displayArea();
    15.  
    16.  
    17.    
    18.    
    19.  
    20.     return 0;
    21. }

    This code works perfectly but when i try to make a member function const like getwidth() for example, the compiler returns an error: 1>z:\college\year2\semester 2\programming\assignment 2 rectangle\assignment 2 rectangle\rectangle definition.cpp(34) : error C2440: 'return' : cannot convert from 'const rectangle' to 'rectangle &'

    Is it not possible to do this at all?
     
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    The compiler is trying to convert from const rectangle to & rectangle. The const rectangle expression is essentially read-only, while the & rectangle expression can be used to modify the fields of the rectangle, and so is at odds with the read-onliness of const rectangle. That's what I think.

    Some comments...
    Your rectangle constructor should not also take an area argument. Doing so makes it possible to create a rectangle instance such as rectangle(1, 2, 5). Obviously you can't have a rectangle whose width is 2 and whose length is 5, whose area is 1. The area should only be calculated, not set.

    Also, why do your getlength, getwidth, and Calculatearea methods return a rectangle (or rectangle reference)? I think it would be more reasonable for them to return the appropriate double values. Also, the displayArea method should probably not return anything (i.e., should be type void).
    Mark
     
  3. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Thanks Mark, indeed I agree with you on all counts. As per your thoughts, I will try using the const method on only read only members and see what it says.

    In relations to your suggestions, I merely impletemented it this way for educational purposes, to see if I could actually get this cascaded function call thing going :)

    Yeah I was thinking the exact same thing when allowing an area to be initialised, I think I was going to add more to the program and never did so now it just looks silly.

    Anyhoo, thanks and I'll let you know if using const on only read only members does the trick.
     
  4. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Well, trying this without returning a reference to the object, that is just returning the object itself works.

    I.e. having a return type of rectangle and not rectangle & allows the use of const member functions.
     
Loading...