A const member function is not supposed to operate the data member.
However, it can operate the data member if it is a reference data type.
Why can operate the reference data member even if the member function is constant?
It is shown below:
two classes: Bird & BirdHouse

BirdHouse composite two Birds one is object and another one is a reference

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Bird
{
	int nb;
public:
	Bird(int n):nb(n)
	{
	}

	void set(int i){ nb = i;}
};

class BirdHouse
{
	Bird& rb;
	Bird b;
	int bhn;
public:
	BirdHouse( Bird& b1,Bird&b2):rb(b1),b(b2)
	{		
		bhn = b1.get();
	}
	void set(int a)const //constant member function
	{
		rb.set(a); //why?reference data member can be assigned a new value
		//b.set(a); //it cannot be operated
	}	
};
int main()
{
}

line 28 the reference can be operated in a constant member function. why?
line 29, the object cannot be operated. This follows the constant member function rules.

thanks in advance

Not sure, as I am a novice myself. But I think "const" is a promise not to change the class members. What are your class members?

Bird b is a permanent class member.

-> Thus b you cannot change when you call set(int) in birdhouse as it is its class member.

What about rb? rb is a reference. A reference is an alias for something. In must refer to another object. It holds the same address as the object it refers to.

The object it is going to refer to is the first passed object when you create BirdHouse. Lets say:

Bird eagle(1);
Bird dove(2);

BirdHouse BH(eagle, dove) ;

Thus you have now a real object named "eagle" and a reference, so to speak another name for it, called "Birdhouse.rb".

Therefore whenever you want to use your eagle, you can write

Eagle.set(6); Eagle.fly(10)

or as an alias, once you create BirdHouse BH:

BH.rb.set(6); BH.rb.fly(10)

As I said Birdhouse.rb has the same address as Bird eagle. But eagle is the origin of the reference. The bird object BrdHouse.rb is therefore not a class member of Birdhouse, only its reference is. Changing Eagle doesn't hurt the class members of Birdhouse.

Hope that helps,

&Cheers,

poliet

Thanks poliet!
I understand what you are saying.

However, I think the const member function makes its data members car const.
for example this case,

the b and br should be const and they are looked like:

const Bird b;
const Bird& br;

therefore,

Bird eagle(1);
eagle.set(2);
const Bird& br = eagle;
//br.set(3);

line 4 cannot be operated.
But it can be in the const member function.
I got confused.

When you write:

const Bird& br = eagle;

or

Bird const & br = eagle;

then basically you want br to be an alias for eagle and it is equipped with the same address as our eagle. But here also the keyword "const" is glued to the reference and not to the object. Thus the reference br is not allowed to be modified.

What is the use of a constant reference? Well, references do not dublicate when passed, thus they are quite efficient in terms of memory management. Passing by value on the other hand can be quite bulky. Still, we DO WANT to protect our source objects and that is why we declare references sometimes to be constant. Maybe the scope of protection might sound not clear from scratch, but when you have hundreds of interdependent classes, constant objects and thousands of lines, they help you to remember where to look for in the code as the compiler alarms you on every violation.

Now, when you write:

const Bird eagle(1);

You say that Bird is an constant object. As such you are not allowed to change it and it cannot call any member functions unless they are specified as constant, e.g. Do()const{};

Also when you write:

const Bird eagle(1);
Bird  & br  = eagle;

You have a constant object Then you want to refer to it with an not constant alias. That's clearly a violation!

You will also get a logical error message, since you have a reference to an object eagle, i.e. alias for eagle (which can change in vlaue and thus its source), however, egale was previously defined that it must not change! In order to solve this riddle:

const Bird eagle(1);
const Bird  & br  = eagle;

and it should be fine....

poliet

Yes, I understand the relationship among the constant, the object and the reference.

I just wonder, whether the

void BirdHouse::set() const;

can be thought as the data members(b and br) of class BirdHouse are applied with const inside the body of the function set() as:

const Bird b;
const Bird& br;

If so, the br cannot be changed.
If not, why not?
That's what I am confused.

Not quite sure what you are asking, but br must be initialized and since you declared it to be a constant reference, you will not be able to change it directly without getting a compiler error. Also within a member func declared to be constant, you cannot change br, at least not with my compiler...

Maybe post the whole code again and ask anew with the exact error message.

What is the function .get()? Its not declared.

bhn = b1.get();

Sorry, it's a mistake. It should not be in.

I did not delete clearly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.