Using a const when returning a reference to an object

Thread Starter

mentaaal

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

header file:
Rich (BB code):
#ifndef rectangle_h
#define rectangle_h

class rectangle
{
public:
    rectangle(double = 0,double = 0, double = 0);
    rectangle & setlength();
    rectangle & setwidth();
    rectangle & getlength();
    rectangle & getwidth();
    rectangle & calculateArea();
    rectangle & displayArea();
private:
    double area, length, width;
};

#endif
definition file:
Rich (BB code):
#include "Rectangle.h"
#include <iostream>
using namespace std;


rectangle::rectangle(double Area, double Length, double Width)
{
    area = Area;
    length = Length;
    width = Width;

}

rectangle &rectangle::calculateArea()
{
    area = length*width;
    return *this;
}

rectangle &rectangle::displayArea() 
{
    cout << "The area of rectangle with length " << length << " and width " << width << " is " << area << endl;
    return *this;
}

rectangle &rectangle::getlength()
{
    cout << "The length of the rectangle is " << length << endl;
    return *this;
}
rectangle &rectangle::getwidth() 
{
    cout << "The width of the rectangle is " << width << endl;
    return *this;
}
test file:
Rich (BB code):
#include "Rectangle.h"
#include <iostream>
using namespace std;

int main()
{
    rectangle rectangle1(1,1,1); //create object with area, width and length of 1
    rectangle1.getlength().getwidth().displayArea();
    
    rectangle rectangle2;
    rectangle2.getlength().getwidth().displayArea();
    rectangle2 = rectangle1;
    cout << "After using a copy contructor\n";
    rectangle2.getlength().getwidth().displayArea();


    
    

    return 0;
}

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?
 

Mark44

Joined Nov 26, 2007
628
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
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
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.
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
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.
 
Top