class A
 {
  int num;
  public:
  A() {}
  A(int _num) : num(_num) {}

  A operator + (const  A & ob)
  {
   return (num+ ob.num);
  }
 };

int main()
{
A ob1(1);
A ob2(2);
A ob3(3);
A ob4=ob1+ob2+ob3;
}
  
My doubt is in ob1+ob2+ob3 . First ob1+ob2 is evaluated and a temporary object is created . The temporary is of const type . Next the operator function of the const object is created . So we have to explicitly state that the function is const otherwise it should result in compilation error right . But the above code executes without any error . Plz enlighten me

Recommended Answers

All 11 Replies

Why would the temporary be const ?

By default all temprary objects are const in nature . Source - Thinking in c++

Addition is left to right in c++. So ob2 and ob3 are added together to form a const A. Then ob1 and that const A returned from the previous addition are added together.

ob4 = ob1.operator+( ob2.operator+(ob3) );

Hope this will clear that up for you.

No you are confusing yourself. This is what happens as pointed out :

A ob4 =  (ob1.operaotr+(ob2)).operator+(ob3);

But I think your problem is that this code :

A operator + (const  A & ob){
   return (num+ ob.num);
 }

This code does NOT return a const object. It returns a temporary object in which
its content could be modified. What it does is promise that the object ob passed
in does not get modified.

@above
Yes what you are saying is correct . But all temporary objects constructed will automatically be made const even though function is not of const type . My doubt is when ob1+ob2+ob3 is executed ob1+ob2 will result in creation of temporary object which is of const type . Const objects can access only const member functions but my code does not satisfy this but still the code is executing fine .

Consider this example : ob1+=ob2+ob3 . In this case ob2+ob3 is executed first and results in creation if a temporary const object . The operator overloaded function for += must declare function argument as const for it to work correctly . If this correct then my code above is worng right . Check out Operator overloading in Thinking in c++ . Whats your general opinion on this concept . Where am i going wrong ?

Temporary objects aren't strictly const. While they act const when trying to pass them to a function, you can still call non-const functions on them.

Consider this:

class A
{
  public:
    A() : num(0) {}
    void setToFive() {num=5;}

    int num;
};

void setAToFive(A& a)
{
  a.num=5;
}

int main()
{
  A().setToFive(); //ok
  setAToFive(A()); //not ok
}

I assume this design choice was made to allow easily calling a function with side-effects on a temporary helper object. I currently can't think of a proper example where this would be useful, though. Although I know that I've made use of this in my own code before.

Operators defined as members are treated like a member function of the left-hand operand with the right-hand operand becoming the parameter to the function. What both nathan and first are saying is that the A object being passed to the operator is the parameter, not the calling object. Declaring a member function as const says that the function will not modify the calling object, it does not affect the passed object.

This:

void someFunc(int)const;

does not mean the same as this:

void someFunc(const int);

The return from ob2+ob3 is a const object. That const object gets passed to the const parameter for ob1.operator+. The temporary object is passed as a parameter and the parameter is defined as const.

@above
But the associativity is left to right

>>But all temporary objects constructed will automatically be made const even though function is not of const type

Well if that was true then your example would have been a compile error wouldn't it?
Temporary object will adapt to the situation.

Yes it is left to right but it isn't how you think it is. The object on the left calls its function with the object on the right as its parameter. So for you += example you would have

ob1.operator+=(ob2.operator+(ob3));

As you can see from this you are never calling a non const function in a const object. The const object keeps getting passed up the ladder. Let me know if you need a better explanation.

If you output num and ob.num in operator+, you can see in which order they're added together. I changed ob1 to be initialized with 4, so that 3 remains unambiguous.
Output:
4 2
6 3

So ob1 and ob2 are added together, then the non-const operator+ is called on the resulting temporary with ob3 as the parameter.

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.