4

I have doubt whether we can do the following or not.

Suppose I have created two instance of class A i.e. obj1 and obj2 and class A has member function show().

Can I use the following?

(obj1+obj2).show()

If yes, how? If no, why it is not possible?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Abhineet
  • 6,459
  • 10
  • 35
  • 53
  • What behavior would you expect from that code? – sharptooth Jun 24 '11 at 09:39
  • Are you asking whether you can invoke a member function on the result of a previous operation (possibly not realising that `op+` there is a function call)? – Lightness Races in Orbit Jun 24 '11 at 09:45
  • I just want to display the total value(i.e. obj1.x+obj2.x) using show method without changing the value of both the instance using above technique... – Abhineet Jun 24 '11 at 09:50
  • I knw we can perform the same using obj3 = obj1+obj2; obj3.show(); But if we can do directly why should we use another instance.... That was my basic point of view..... – Abhineet Jun 24 '11 at 09:51

7 Answers7

8

Yes it is possible, just implement operator+ for A and have it return a class of type A:

#include <iostream>

class A
{
public:
    explicit A(int v) : value(v) {}

    void show() const { std::cout << value << '\n'; }

    int value;
};

A operator+(const A& lhs, const A& rhs)
{
    A result( lhs.value + rhs.value );
    return result;
}

int main()
{
    A a(1);
    A b(1);

    (a+b).show(); // prints 2!

    return 0;
}
Adam Bowen
  • 10,820
  • 6
  • 36
  • 41
2

You can do it if you overload the + operator in such a way that it takes two arguments of type A and yields an object that has a method named show.

Oswald
  • 31,254
  • 3
  • 43
  • 68
2

If obj1+obj2 does return an object that have a show() function member, then yes it's possible.

If not, it is not.

So, it depends on the operator+ function that is used here, that depends on both types of obj1 and obj2.

obj1+obj2 is an expression that have a type, the type of the object returned by the operation, like any expression. Now, once the expression is executed, you have this object. But as here you don't associate it to a name (using assignation for example), it is a "temporary", meaning it will be destroyed at the end of the full expression.

So, if the resulting temporary object's type does provide a show() function then you can call it like you did.

If it don't provide a show() function, then you're trying to call a function that don't exists.

So in any case, the compiler will stop you, it will not be a runtime error.

I would be you, I would setup a minimal test project just to play with those principles.

Klaim
  • 67,274
  • 36
  • 133
  • 188
  • Why does it need to be `const`? A temporary is **not** `const`. [Look at this](http://www.ideone.com/u4Z1t). Perhaps you're getting confused because you cannot bind temporaries to `refs-to-non-const` (which is a completely different issue). – Lightness Races in Orbit Jun 24 '11 at 09:46
  • "if [+] return an object [having] constant show()" - this limitation is only true if `operator+` returns a `const` object, which is often sensible (in line with C++ generally not allowing temporaries to be lvalues) but optional. – Tony Delroy Jun 24 '11 at 09:47
  • Interesting, AFAIK it shouldn't... Or maybe it's because of optimizations (inlining)? As specified by the +operator, the returning object is a temporary, so it shouldn't be modifiable. show isn't const so it should not be callable there... – Klaim Jun 24 '11 at 09:48
  • @Klaim: Can you provide some evidence for your assertion that temporaries cannot be modified? That seems like nonsense to me. – Lightness Races in Orbit Jun 24 '11 at 09:49
  • @Tony: temporaries are implicitely const...right? It depends on how the operator is called and here there is no assignation so it's really a temporary.....am I right? – Klaim Jun 24 '11 at 09:49
  • Sorry I'm wrong, there is a nuance : temporaries can only be referenced with const references, but that don't make them const. I'll modify the answer. – Klaim Jun 24 '11 at 09:53
  • @Klaim: Sorry, but no - not for objects, where the programmer's "God" and gets to decide, having `operator+` return a `const` object or not. Nothing to do with inlining either. – Tony Delroy Jun 24 '11 at 09:54
  • Yes, sorry, I mixed with reference assignation and life extension rules :) – Klaim Jun 24 '11 at 09:55
  • @Klaim: Apologies, seems you're right. They _are_ non-modifiable in C++03. Come join us in the C++ chat room where we're discussing it. – Lightness Races in Orbit Jun 24 '11 at 09:56
  • Wait what? XD Ok I'm joining but IO'm not sure the company proxy will allow it... – Klaim Jun 24 '11 at 09:57
  • @Klaim: Sorry again, but [you _are_ wrong](http://stackoverflow.com/questions/6466253/if-temporaries-are-implicitly-const-how-does-this-work/6466316#6466316). The call to member function `show` should be fine. – Lightness Races in Orbit Jun 24 '11 at 10:08
2

Yes you can use it if you have overloaded the + operator of class A to return an obect of class A.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

Write an operator overload for + operator and define the operation you want on that.

In the operator overload for + operator just update the logic you want like adding the individual member variables or concatenating the names etc meaningfully based on your use case

Then you can call That new object.Show() function just like an object calling member function.

ckv
  • 10,539
  • 20
  • 100
  • 144
0

Depends on what the result type of obj1+obj2 is and whether .show() is a constant method.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • 1
    [`show()` doesn't need to be `const`.](http://stackoverflow.com/questions/6466253/if-temporaries-are-implicitly-const-how-does-this-work/6466316#6466316) – R. Martinho Fernandes Jun 24 '11 at 10:10
-2

No! The result of (obj1+obj2) isn't object. You may overload "=" and use:

obj3 = obj1 + obj2;
obj3.show();
G-71
  • 3,626
  • 12
  • 45
  • 69