5

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?

4
  • What behavior would you expect from that code? Commented Jun 24, 2011 at 9: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)? Commented Jun 24, 2011 at 9: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... Commented Jun 24, 2011 at 9: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..... Commented Jun 24, 2011 at 9:51

7 Answers 7

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

2

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

Comments

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.

11 Comments

Why does it need to be const? A temporary is not const. Look at this. Perhaps you're getting confused because you cannot bind temporaries to refs-to-non-const (which is a completely different issue).
"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.
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: Can you provide some evidence for your assertion that temporaries cannot be modified? That seems like nonsense to me.
@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?
|
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.

Comments

0

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

1 Comment

-2

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

obj3 = obj1 + obj2;
obj3.show();

1 Comment

Why not? If you can write obj3 = obj1 + obj2 then do stuff with obj3, then you may absolutely write (obj1 + obj2).show().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.