2

I implemented an example class Demo, with an overloaded function run. The function run has a different implementation depending on whether it is called from an immutable instance of an object or a mutable instance. Is it also possible to create yet another version of run that runs when we call it from an anonymous function? i.e.,

Demo().run;

Here is the code so far.

#include <iostream>

using namespace std;

class Demo {
  public:
   Demo() { }

   void run() { cout << "Called from object or from function expecting a reference to object" << '\n'; }
   const void run() const { cout << "Called from const object" << '\n'; }
 };

int main(){
    const Demo obt;
    Demo obt2;
    obt2.run();
    obt.run();
    Demo().run();
}

The output is

Called from object or from function expecting a reference to object
Called from const object
Called from object or from function expecting a reference to object

so right now the Demo().run() sees the temporary anonymous class Demo() as a normal class.

4
  • note that const void is a bit misleading, since you cannot have different overloads based on just return type anyway Commented Oct 4, 2016 at 12:37
  • 1
    @SamVarshavchik Good call. Out of curiosity, why don't you just close it as a duplicate? Commented Oct 4, 2016 at 12:45
  • Judgement call. I'm only about 99% sure that it's a duplicate. Commented Oct 4, 2016 at 12:47
  • From what I see the possible duplicate, provides the solution to the problem here. Commented Oct 4, 2016 at 12:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.